Javascript logic judgment for IP addresses, subnet masks, and gateways _ javascript skills

Source: Internet
Author: User
This article mainly introduces the logic judgment of js for IP addresses, subnet masks, and gateways. If you are interested, refer to the js verification for static address configuration, I found a lot of information about ip addresses and masks on the Internet. I didn't have ip addresses, submask, or gateway logic judgments. I wrote my own code for reference.

Common knowledge about Gateway addresses:

First:And calculate 1 and 1, and 0 are 0, 0 and 0 are 0. First, expand the ip address and subnet mask.
10.70.64.223 00001010. 01000110.0000000.11011111
255.255.255. 0 111111111.11111111.11111111.00000000
The network segment is 00001010. 01000110.0000000.00000000.
Then convert it to the decimal format: 10.70.64.0.

Second point:The IP address and subnet mask are the same as the operation, and the gateway address and subnet mask are the same as the calculation result, that is, the host number is the same.
Here, I first use js to separate ip addresses, masks, and gateways by '.' And then make judgments.

Third point:Js bitwise AND OPERATIONS

Result = [integer 1] & [integer 1]
& Perform the bitwise "and" operation on each bit of two 32-bit expressions. If both bits are 1, the result is 1. Otherwise, the result is 0.

ShareJs logic judgment on IP addresses, subnet masks, and gatewaysCode details

Function checkIP (ip) {obj = ip; var exp =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $/; var reg = obj. match (exp); if (reg = null) {return false; // invalid} else {return true; // valid} function checkMask (mask) {obj = mask; var exp =/^ (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 \. 0 \. 0 | 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 \. 0 | 255 \. 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 | 255 \. 255 \. 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0) $/; var reg = obj. match (exp); if (reg = null) {return false; // "invalid"} else {return true; // "valid"} var static_ip = document. getElementById ('static _ ip '). value; var static_mask = document. getElementById ('static _ mask '). value; var static_gw = document. getElementById ('static _ gw '). value; if (static_ip = '') {// $ (" # Static_ip_error ").css (" display "," block "); document. getElementById ('static _ ip'). focus (); return false;} else if (! CheckIP (static_ip) {// $ ("# static_ip_error" ).css ("display", "none"); document. getElementById ('static _ ip '). focus (); return false;} if (static_mask = '') {// $ (" # static_mask_error "..css (" display "," block "); document. getElementById ('static _ mask '). focus (); return false;} else if (! CheckMask (static_mask) {// $ ("# static_mask_error" ).css ("display", "none"); document. getElementById ('static _ mask '). focus (); return false;} if (static_gw = '') {// $ (" # static_gw_error "..css (" display "," block "); document. getElementById ('static _ gw '). focus (); return false;} else if (! CheckIP (static_gw) {// $ ("# static_gw_error" ).css ("display", "none"); document. getElementById ('static _ gw '). focus (); return false;} if (static_ip = static_mask | static_mask = static_gw) {alert ('address input error! '); Return false; // The three addresses cannot be the same} var static_ip_arr = new Array; var static_mask_arr = new Array; var static_gw_arr = new Array; static_ip_arr = static_ip.split (". "); static_mask_arr = static_mask.split (". "); static_gw_arr = static_gw.split (". "); var res0 = parseInt (lan_ip_arr [0]) & parseInt (static_mask_arr [0]); var res1 = parseInt (lan_ip_arr [1]) & parseInt (static_mask_arr [1]); var res2 = parseInt (lan _ Ip_arr [2]) & parseInt (static_mask_arr [2]); var res3 = parseInt (lan_ip_arr [3]) & parseInt (static_mask_arr [3]); var res0_gw = parseInt (static_gw_arr [0]) & parseInt (static_mask_arr [0]); var res1_gw = parseInt (region [1]) & parseInt (static_mask_arr [1]); var res2_gw = parseInt (static_gw_arr [2]) & parseInt (static_mask_arr [2]); var res3_gw = parseInt (region [3]) & parseInt (static_mask_arr [3] ); If (res0 = res0_gw & res1 = res0000gw & res2 = res2_gw & res3 = res3_gw) {} else {alert ('IP address does not match the subnet mask and gateway address! '); Return false ;}

Share the following code to verify the validity of the IP address and subnet mask in js:

Function checkIP (ip) {obj = ip; var exp =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) \. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) $/; var reg = obj. match (exp); if (reg = null) {return false; // invalid} else {return true; // valid} function checkMask (mask) {obj = mask; var exp =/^ (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 \. 0 \. 0 | 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 \. 0 | 255 \. 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0 )\. 0 | 255 \. 255 \. 255 \. (254 | 252 | 248 | 240 | 224 | 192 | 128 | 0) $/; var reg = obj. match (exp); if (reg = null) {return false; // "invalid"} else {return true; // "valid "}}

The above is all the content of this article, hoping to help you learn.

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.