Home > Article > Web Front-end > How to check whether the IP address is legal in javascript
Javascript method to verify whether the IP address is legal: first create a js sample file; then verify whether the IP address is legal through js regular expressions, code such as "function isValidIP(ip){...}" .
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
How does JavaScript verify whether the IP address is legal?
js regular expression to verify legal IP address
The code is as follows:
function isValidIP(ip) { var reg = /^(\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])$/ return reg.test(ip); } let a = isValidIP('10.10.10.1111') console.log(a)
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to check whether the IP address is legal in javascript. For more information, please follow other related articles on the PHP Chinese website!