Home >Web Front-end >Front-end Q&A >How to verify whether the ip is accessible in javascript
JavaScript is a widely used programming language that can help us quickly develop highly interactive applications on websites. In website development, verifying IP addresses is a common task. Let's explore how to use JavaScript to verify IP addresses.
IPv4 is a common IP address type, which consists of four numbers separated by periods, each number ranging from 0 to 255. JavaScript can be used to verify the legitimacy of IPv4 addresses through regular expressions.
The following is a code example that can be used to verify IPv4 addresses:
function validateIPv4Address(ipAddress) { var ipv4Pattern = /^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; return ipv4Pattern.test(ipAddress); } // 示例 console.log(validateIPv4Address('192.168.0.1')); // true console.log(validateIPv4Address('')); // false console.log(validateIPv4Address('256.168.0.1')); // false
We use a regular expression to match the format of the IP address, and return true if the match is successful, otherwise return false . In regular expressions, we use the pipe character (|) to indicate multiple matching patterns, and the question mark (?) to indicate that there can be 0 or 1 symbols.
IPv6 is a new IP address type that consists of eight hexadecimal digits separated by colons. Since IPv6 addresses are more complex than IPv4 addresses, we need to use more complex regular expressions to verify the legitimacy of IPv6 addresses.
The following is a code example that can be used to verify IPv6 addresses:
function validateIPv6Address(ipAddress) { var ipv6Pattern = /^[a-fA-F0-9]{1,4}(:[a-fA-F0-9]{1,4}){7}$/; return ipv6Pattern.test(ipAddress); } // 示例 console.log(validateIPv6Address('2001:0db8:85a3:0000:0000:8a2e:0370:7334')); // true console.log(validateIPv6Address('')); // false console.log(validateIPv6Address('2001::7334')); // false
In the above example, we used a regular expression to match the IPv6 address. The character class [a-fA-F0-9] is used in regular expressions to represent the allowed characters. We have used colons (:) to separate hexadecimal digits and curly braces ({}) to indicate a symbol length limit.
Sometimes we need to verify whether an IP address is within a certain IP segment. For example, we may need to restrict access to our website by IP addresses from certain areas. The following is a sample code that can be used to determine whether an IP is within a certain IP segment:
function validateIpInRange(ipAddress, ipRange) { var startIp = ipRange.split('-')[0]; var endIp = ipRange.split('-')[1]; function convertIpToNumber(ipAddress) { return ipAddress.split('.').reduce(function (result, octet) { return (result << 8) + parseInt(octet, 10); }, 0) >>> 0; } var startIpNumber = convertIpToNumber(startIp); var endIpNumber = convertIpToNumber(endIp); var ipNumber = convertIpToNumber(ipAddress); return ipNumber >= startIpNumber && ipNumber <= endIpNumber; } // 示例 console.log(validateIpInRange('192.168.0.1', '192.168.0.0-192.168.0.255')); // true console.log(validateIpInRange('192.168.1.1', '192.168.0.0-192.168.0.255')); // false
In the above example, we defined a function convertIpToNumber, which is used to convert an IP address string into an A number of type 32-bit unsigned integer. By converting IP address strings into numbers, we can compare the relative size of two IP addresses.
We parse the target IP address, starting IP address and ending IP address at the same time in the function. Using these parsed numbers, we can determine whether the target IP address is within a given IP range.
The above is the detailed content of How to verify whether the ip is accessible in javascript. For more information, please follow other related articles on the PHP Chinese website!