Last update on August 19 2022 21:50:37 (UTC/GMT +8 hours)
IP address validation
Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. An IP address consists of four numbers (each between 0 and 255) separated by periods. The format of an IP address is a 32-bit numeric address
written as four decimal numbers (called octets) separated by periods; each number can be written as 0 to 255 (e.g., 0.0.0.0 to 255.255.255.255).
Example of valid IP address
115.42.150.37
192.168.0.1
110.234.52.124
Example of invalid IP address
210.110 – must have 4 octets
255 – must have 4 octets
y.y.y.y – the only digit has allowed
255.0.0.y – the only digit has allowed
666.10.10.20
– digit must between [0-255]
4444.11.11.11 – digit must between [0-255]
33.3333.33.3 – digit must between [0-255]
JavaScript code to validate an IP address
function ValidateIPaddress(ipaddress)
{
if (/^(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]?)$/.test(myForm.emailAddr.value))
{
return (true)
}
alert("You have entered an invalid IP address!")
return (false)
}
Explanation of the said Regular expression (IP address)
View the Javascript IP address validation in the browser
file_download
Download the validation code from here.
Other JavaScript Validation:
Checking for non-empty
Checking for all letters
Checking for all numbers
Checking for floating numbers
Checking for letters and numbers
Checking string length
Email
Validation
Date Validation
A sample Registration Form
Phone No.
Validation
Credit Card No. Validation
Password Validation
IP address Validation
Previous: JavaScript : HTML Form validation - checking for password Next: JavaScript Cookies
JavaScript: Tips of the Day
The unary operator
let num = 10;
const increaseNumber = () => num++;
const increasePassedNumber = number => number++;
const num1 = increaseNumber();
const num2 = increasePassedNumber(num1);
console.log(num1);
console.log(num2);
The unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of num1 is 10, since the increaseNumber function first returns the value of
num, which is 10, and only increments the value of num afterwards. num2 is 10, since we passed num1 to the increasePassedNumber. number is equal to 10(the value of num1. Again, the unary operator ++ first returns the value of the operand, then increments the value of the operand. The value of number is 10, so num2 is equal to 10.