JS 验证:
ipv4地址验证:
function queryDev(ip){
var exp=/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
var flag = ip.match(exp);
if(flag != undefined && flag!=""){
return true;
} else {
return false;
}
}
<input type="text" name="iporname" id="iporname" style="width:80px;" value="" />
<input name="Submit" type="button" value="验证IPV4" οnclick=alert(queryDev(iporname.value)) style="width:80px"/>
验证IPV6地址:
function queryDev(ipvalue){
var isIpv6 = false;
if(ipvalue.match(/:/g).length<=7&&/::/.test(ipvalue)?/^([\da-f]{1,4}(:|::)){1,6}[\da-f]{1,4}$/i.test(ipvalue):/^([\da-f]{1,4}:){7}[\da-f]{1,4}$/i.test(ipvalue)){
isIpv6 = true;
}
if(isIpv6){
return true;
} else {
return false;
}
}
<input type="text" name="iporname" id="iporname" style="width:80px;" value="" />
<input name="Submit" type="button" value="验证IPV6" οnclick=alert(queryDev(iporname.value)) style="width:80px"/>
====================================================================================================================================
JAVA 验证 IPV4,ipv6地址
/***
* @param ip* @return
*/
public static boolean validata_ipv6(String host){
InetAddress addressIpv6 = null;
Inet6Address IPv6 = null;
Inet4Address Ipv4 = null;
try {
addressIpv6 = InetAddress.getByName(host);
} catch (UnknownHostException e) {
e.printStackTrace();return false;
}
if(addressIpv6 instanceof Inet6Address){
IPv6 = (Inet6Address) addressIpv6;
return true;
}
if (addressIpv6 instanceof Inet4Address)
{return false;}
return true;
}
IPV4:
/***
*@param ip
* @return
*/
public static boolean validata_ipv4(String host){
InetAddress addressIpv4 = null;
Inet6Address IPv6 = null;
Inet4Address Ipv4 = null;
try {
addressIpv4 = InetAddress.getByName(host);
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
}
if(addressIpv4 instanceof Inet6Address){
IPv6 = (Inet6Address) addressIpv4;
return false;
}
if(addressIpv4 instanceof Inet4Address){
IPv4 = (Inet4Address) addressIpv4;
return true;}
return true;
}
====================================================================================================================================
JAVA 验证 IPV4,ipv6地址
/***
* @param ip* @return
*/
public static boolean validata_ipv6(String host){
InetAddress addressIpv6 = null;
Inet6Address IPv6 = null;
Inet4Address Ipv4 = null;
try {
addressIpv6 = InetAddress.getByName(host);
} catch (UnknownHostException e) {
e.printStackTrace();return false;
}
if(addressIpv6 instanceof Inet6Address){
IPv6 = (Inet6Address) addressIpv6;
return true;
}
if (addressIpv6 instanceof Inet4Address)
{return false;}
return true;
}
IPV4:
/***
*@param ip
* @return
*/
public static boolean validata_ipv4(String host){
InetAddress addressIpv4 = null;
Inet6Address IPv6 = null;
Inet4Address Ipv4 = null;
try {
addressIpv4 = InetAddress.getByName(host);
} catch (UnknownHostException e) {
e.printStackTrace();
return false;
}
if(addressIpv4 instanceof Inet6Address){
IPv6 = (Inet6Address) addressIpv4;
return false;
}
if(addressIpv4 instanceof Inet4Address){
IPv4 = (Inet4Address) addressIpv4;
return true;}
return true;
}