JS及java验证 IPV6,IPV4地址的 正则表达式

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;
    }



 
 
 
 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值