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

How to check whether the IP address is legal in javascript

藏色散人
Release: 2021-06-21 11:21:24
Original
4591 people have browsed it

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)
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template