Phone Number Validation is the process of verifying whether the phone number entered by the user follows a predefined format before submitting the form.
In JavaScript, Regular Expressions (RegEx) provide an efficient and powerful way to validate phone numbers by matching user input against a specific pattern.
Regular Expressions are used because they:
- Validate input quickly.
- Reduce lengthy conditional statements.
- Check complex patterns with a single expression.
- Improve code readability.
- Provide accurate input validation.
phone_regex.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Phone Number Validation using Regular Expressions</title>
<script>
function validate() {
var res =
document.getElementById("phone").value;
var t =
/^[6-9]{1}[0-9]{9}$/.test(res);
if(t == false) {
document.getElementById("msg").innerHTML =
"Invalid phone number";
document.getElementById("phone").style.border =
"2px solid red";
return false;
}
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>/^[6-9]{1}[0-9]{9}$/This is the heart of the program.
Let us break it down:
Beginning of the string
The phone number must start from here.
First digit should be 6, 7, 8 or 9
Examples:
✔ 9876543210
✔ 8123456789
✘ 5123456789
Followed by exactly 9 digits
Examples:
✔ 9876543210
✘ 98765432
✘ 987654321012
End of the string
Ensures there are no extra characters after 10 digits.
/^[6-9]{1}[0-9]{9}$/Means:
The number must:
Start with 6,7,8 or 9
AND
Contain exactly 10 digits
AND
Contain only numbers
regex.test(string)Returns:
true -> Pattern matched
false -> Pattern not matched
Example:
var exp = /^[6-9]{1}[0-9]{9}$/;
console.log(exp.test("9876543210"));Output:
true
Example:
console.log(exp.test("1234567890"));Output:
false
var res =
document.getElementById("phone").value;Gets the user entered phone number.
var t =
/^[6-9]{1}[0-9]{9}$/.test(res);Checks whether the phone number matches the regular expression.
If matched:
t = true
Else:
t = false
if(t == false)Checks whether the input is invalid.
document.getElementById("msg").innerHTML =
"Invalid phone number";Displays:
Invalid phone number
document.getElementById("phone").style.border =
"2px solid red";Changes the border color to red.
Output:
+-------------------+
| Invalid Input |
+-------------------+
Border becomes RED
return false;Stops form submission.
return true;Allows form submission.
Input:
9876543210
Output:
Valid Phone Number
Form Submitted Successfully.
Input:
5876543210
Output:
Invalid phone number
Reason:
Starts with 5
Input:
98765432
Output:
Invalid phone number
Reason:
Less than 10 digits
Input:
987654321012
Output:
Invalid phone number
Reason:
More than 10 digits
Input:
98ABC43210
Output:
Invalid phone number
Reason:
Contains alphabets
| Symbol | Meaning |
|---|---|
| ^ | Start of string |
| $ | End of string |
| [] | Range of characters |
| {} | Number of occurrences |
| . | Any single character |
| * | Zero or more occurrences |
| + | One or more occurrences |
| ? | Optional character |
Answer:
A Regular Expression is a sequence of characters used to define a search pattern for matching, validating, and manipulating strings.
Answer:
test()Example:
regex.test(str)Answer:
It indicates:
Start of the string
Answer:
It indicates:
End of the string
/^[6-9]{1}[0-9]{9}$/Answer:
The phone number:
- Must start with 6, 7, 8, or 9.
- Must contain exactly 10 digits.
- Must contain only numbers.
Answer:
true -> if pattern matches
false -> if pattern does not match
Answer:
element.style.borderExample:
document.getElementById("phone").style.border =
"2px solid red";- Regular Expressions are used for pattern matching.
test()checks whether input matches a pattern.^indicates the beginning of the string.$indicates the end of the string.- Indian mobile numbers generally start with 6, 7, 8, or 9.
- Client-side validation improves user experience.
- Server-side validation should also be implemented for security.
Phone Number Validation using Regular Expressions is one of the most important applications of JavaScript form validation. Using the expression:
/^[6-9]{1}[0-9]{9}$/developers can efficiently validate Indian mobile numbers with minimal code. Regular Expressions provide fast, flexible, and reliable validation techniques that are widely used in modern web applications for validating phone numbers, emails, passwords, and other user inputs.