This article is a mirror article of machine translation, please click here to jump to the original article.

View: 9562|Reply: 0

[JavaScript] js intercepts the last one. can be used to judge the IP segment range

[Copy link]
Posted on 9/27/2015 11:57:57 PM | | |
Since we need to configure IP authentication in our project (only the network address with the IP address configured in the database can access our interface address, otherwise there will be no this permission), because there are more vendors that need to be accessed, the IP address may belong to a certain end, For example, maybe a vendor has 127.0.0.1, 127.0.0.2, 127.0.0.3, 127.0.0.4, 127.0.0.5, 127.0.0.6 We only need to configure 127.0.0.* to allow access, that is, we only need to judge the first three digits. However, when I make an IP authentication list on the page, I need to use the IP address as a query condition to query.

   There are many IP addresses on the Internet to judge, but when I do a query, I also need to enter the 127.0.0.* address to judge, but such an IP address obviously does not match the IP address, and then I thought of whether I could first intercept only the first three digits on the page and then judge only the first three digits of the IP address, so I started to practice. The code is as follows:

  1. 1.function   checkIP()   
  2. 2.{   
  3. 3.     var   sIPAddress=document.getElementById("ipAddr").value;   
  4. 4.     var ipAddr = sIPAddress.substring(0, sIPAddress.lastIndexOf('.'));     //截取IP地址中最后一个.前面的数字   
  5. 5.  
  6. 6.    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])$/;   
  7. 7.      var   reg   =   ipAddr.match(exp);   
  8. 8.      var   ErrMsg= "你输入的是一个非法的IP地址段!\nIP段为::xxx.xxx.xxx.xxx(xxx为0-255)\n或xxx.xxx.xxx.*(xxx为0-255)! ";            
  9. 9.      if(sIPAddress!=""){   
  10. 10.           if(reg==null)   
  11. 11.           {   
  12. 12.        alert(ErrMsg);   
  13. 13.        return false;   
  14. 14.           }   
  15. 15.      }   
  16. 16.        return true;   
  17. 17.}   
Copy code






Previous:XMLHttpRequest cannot load ''. No 'Access-Control-Allow-Origin'
Next:UCloud vulnerability handling process and reward details
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com