Mehul Kuriya Mehul Kuriya - 2 years ago 131
PHP Question

Jquery validaton for mobile

How to validate the mobile number using jquery validator... Its accept only 123-456-7890 Or 1234567890 other than this format should return error.

Following are the sample code:

<input type = "text" id = "mobile" />
<input type = "submit" id = "btnValidate" value = "Submit" onclick = "return Validate()" />

<script type = "text/javascript">
function Validate() {
var mobile = document.getElementById("mobile").value;
var pattern = /^\d{10}$/;
if (pattern.test(mobile)) {
alert("Your mobile number : " + mobile);
return true;
alert("It is not valid mobile number.");
return false;

Any help will be appreciated

Answer Source

Please try following code for the jquery validation with regex code.

$.validator.addMethod("mobile", function(value, element) {
    return this.optional(element) || /^\(?[0-9]{3}\)?[-]?([0-9]{3})[-]?([0-9]{4})+$/i.test(value);
}, "Enter valid moobile number( Eg: 123-456-7890 OR 1234567890).")

This code may help you...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download