Javascript implements verification of IP address and other related information code, javascriptip

Source: Internet
Author: User

Javascript implements verification of IP address and other related information code, javascriptip

This code is extracted from a personal project. It can be regarded as a fairly comprehensive and effective verification code for IP-related information for front-end verification.

/******************* // Determines whether the IP address is valid */var judgeIpIsLegal = function (ipAddr) {var regIps =/^ (25 [0-5] | 2 [0-4] \ d | 1 \ d {2} | [1-9] \ d | [0-9]) \.) {3} (25 [0-5] | 2 [0-4] \ d | 1 \ d {2} | [1-9] \ d | [0-9] )) $/; return regIps. test (ipAddr);}/* convert the IP address to a binary string * // * For example: 172.16.4.235 --> 10101100000100000000010011101011 */var praseIpToBinary = function (ipAddress) {var numArray = ipAddress. split (". "); if (numArray. length! = 4) {alert ("Incorrect IP address entered"); return;} var returnIpStr = ""; for (var I = 0; I <4; I ++) {var curr_num = numArray [I]; var number_Bin = parseInt (curr_num); number_Bin = number_Bin.toString (2); var iCount = 8-number_Bin.length; for (var j = 0; j <iCount; j ++) {number_Bin = "0" + number_Bin;} returnIpStr + = number_Bin;} return returnIpStr ;} /* determine whether the subnet mask is valid * // * The subnet mask must be a continuous segment consisting of 1 and 0, such as 11110000 */var judgeS UbnetMask = function (ipAddress) {var binaryIpString = praseIpToBinary (ipAddress ). toString (); var subIndex = binaryIpString. lastIndexOf ("1") + 1; var frontHalf = binaryIpString. substring (0, subIndex); var backHalf = binaryIpString. substring (subIndex); if (frontHalf. indexOf ("0 ")! =-1 | backHalf. indexOf ("1 ")! =-1) {return false;} else {return true;}/* two IP addresses and operation return results * // * this function is mainly used to implement IP address and subnet mask phase, obtain the IP address segment of the current IP Address * // * to verify whether the entered gateway address is valid */var getIPsAndResult = function (ipAddr1, ipAddr2) {var ipArray1 = ipAddr1.split (". "); var ipArray2 = ipAddr2.split (". "); var returnResult =" "; if (ipArray1.length! = 4 | ipArray2.length! = 4) {alert ("Incorrect IP address entered"); return ;}for (var I = 0; I <4; I ++) {var number1 = parseInt (ipArray1 [I]); var number2 = parseInt (ipArray2 [I]); returnResult + = number1 & number2; if (I <3) {returnResult + = ". ";}} return returnResult;}/* determine whether the gateway address is valid */var judgeGatewayResult = function (ipAddr, subnetMask, gateway) {var andResult1 = getIPsAndResult (ipAddr, subnetMask ); var andResult2 = getIPsAndResult (gateway, subnetMask); if (andResult1 = andResult2) {return true;} else {return false ;}}

The above is all the content of this article. I hope you will like it.

Related Article

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.