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