Verify the Regular Expression of IPv6 and IPv4 addresses in JS and Java

Source: Internet
Author: User

JS Verification:

IPv4 address verification:

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 =" verify IPv4 "onclick = alert (querydev (iporname. value) style = "width: 80px"/> verify the IPv6 address:

Function querydev (ipvalue) {var isipv6 = false; If (ipvalue. Match (/:/g). Length <= 7 &/:/. Test (ipvalue )? /^ ([\ Da-F] {} (:|::) {} [\ da-F] {} $/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 =" verify IPv6 "onclick = alert (querydev (iporname. value) style = "width: 80px"/>

========================================================== ========================================================== ========================================================== ============== Java verifies IPv4, IPv6 address/***** @ 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) failed; return false;} If (addressipv4 instanceof inet4address) {IPv4 = (inet4address) addressipv4; return true;} return true ;} ========================================================== ========================================================== ========================================================== ============== Java verifies IPv4, IPv6 address/***** @ 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) failed; return false;} If (addressipv4 instanceof inet4address) {IPv4 = (inet4address) addressipv4; return true;} return true ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.