
JavaScript regex validate Telephone no.

<html>
<head>
<title>Phone number validation using regex</title>
<script type="text/javascript">
function validate() {
var phone = document.getElementById("phone").value;
var pattern = /^\d{3,5}([\-]\d{6,8})?$/;
if (pattern.test(phone)) {
alert("Your phone number : "+phone);
return true;
}
alert("It is not valid phone number!");
return false;
}
</script>
</head>
<body>
<h2>Validating phone number..</h2>
Enter Phone No. :
<input type="text" name="phone" id="phone" />
<input type="submit" value="Check" onclick="validate();" />
</body>
</html>

The above example validate 10 digit (eg-9675647324) mobile number by using regular expression-
/^\d{3,5}([-]\d{6,8})$/; In this pattern u can put 3 to 5 length
of area code and â?? as a separator and 6 to 8 length of number. If you want to fix length 4 then put \d{4}.
Here /../(forward slashes) is used to quote your regular expression.
^ shows beginning of string.
\d{3,5} shows any digit 0-9 ranges 3 to 5.
[-] is used to put -.
$ is used for ending the string.
test() method takes one argument and check that to the pattern.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.