Phone Number Validation is the process of verifying whether the phone number entered by the user satisfies the required conditions before submitting the form.
JavaScript performs this validation on the client side, allowing users to correct mistakes immediately without sending invalid data to the server.
Phone Number Validation is used to:
- Ensure the field is not empty.
- Accept only numeric values.
- Restrict the phone number length.
- Prevent invalid data submission.
- Improve user experience.
- Reduce unnecessary server requests.
phone.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Phone Number Validation</title>
<script>
function validate() {
var res =
document.getElementById("phone").value;
if(res.length == 0) {
document.getElementById("msg").innerHTML =
"Phone number is required";
return false;
}
else if(isNaN(res)) {
document.getElementById("msg").innerHTML =
"Phone number must be numeric";
return false;
}
else if(res.length > 10) {
document.getElementById("msg").innerHTML =
"Phone number must be less than or equal to 10 digits";
return false;
}
else {
return true;
}
}
</script>
</head>
<body>
<form onsubmit="return validate()">
<label>Phone Number</label>
<input type="text"
id="phone"
name="phone">
<span id="msg"
style="color:red"></span>
<br><br>
<input type="submit"
value="Submit">
</form>
</body>
</html>function validate()Creates a validation function.
This function executes whenever the user clicks the Submit button.
var res =
document.getElementById("phone").value;Retrieves the value entered by the user.
Example:
9876543210
The value is stored in variable:
resif(res.length == 0)Checks whether the phone number field is empty.
If true:
document.getElementById("msg").innerHTML =
"Phone number is required";Output:
Phone number is required
isNaN(res)isNaN() stands for:
is Not a Number
It checks whether the entered value is numeric.
Example:
987ABC
Output:
Phone number must be numeric
res.length > 10Checks whether the entered phone number exceeds 10 digits.
Example:
987654321012
Output:
Phone number must be less than or equal to 10 digits
return false;Stops form submission.
The browser remains on the same page until valid data is entered.
return true;Allows the form to submit successfully.
isNaN(value)Returns:
| Input | Output |
|---|---|
| "123" | false |
| "98765" | false |
| "ABC" | true |
| "123ABC" | true |
| "" | false |
Example:
console.log(isNaN("123"));Output:
false
Example:
console.log(isNaN("Hello"));Output:
true
<form onsubmit="return validate()">Explanation:
- User clicks Submit.
- validate() function is called.
- If:
return trueForm submits.
If:
return falseSubmission stops.
Used to access HTML elements.
Example:
document.getElementById("phone")Returns user entered data.
Example:
document.getElementById("phone").valueDisplays dynamic messages.
Example:
document.getElementById("msg").innerHTML =
"Invalid Phone Number";Input:
(empty)
Output:
Phone number is required
Input:
abcde
Output:
Phone number must be numeric
Input:
9876abc123
Output:
Phone number must be numeric
Input:
987654321012
Output:
Phone number must be less than or equal to 10 digits
Input:
9876543210
Output:
Form Submitted Successfully
Answer:
Form Validation is the process of checking user inputs before sending them to the server.
Answer:
Validation performed in the browser using JavaScript is called Client-Side Validation.
Answer:
isNaN() checks whether a value is Not a Number.
Syntax:
isNaN(value)Answer:
It prevents the form from being submitted.
Answer:
onsubmitAnswer:
document.getElementById()Answer:
value- JavaScript performs Client-Side Validation.
- Phone numbers are usually validated using
isNaN(). return falseprevents form submission.return trueallows form submission.getElementById()accesses HTML elements.innerHTMLdisplays dynamic error messages.- Validation improves user experience.
- Server-side validation is still necessary for security.
Phone Number Validation is one of the most common applications of JavaScript Form Validation. It ensures that the user enters valid numeric data with the required length before submitting the form. By combining DOM methods such as getElementById(), value, innerHTML, and functions like isNaN(), developers can build responsive and user-friendly validation systems for modern web applications.