Home  >  Article  >  Web Front-end  >  How to check whether the IP address is legal in javascript

How to check whether the IP address is legal in javascript

藏色散人
藏色散人Original
2021-06-21 11:21:244472browse

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){...}" .

How to check whether the IP address is legal in javascript

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn