Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.19/5 (4 votes)
See more:
how to validate phone and mobile number in javascript.
for example phone no -044 27221343. mobile no-9944034466/8428664422
/7666544221
Posted
Updated 19-Mar-20 20:37pm

Javascript also supports RegExp (regular expressions). I would suggest using Expresso's phone number regexp examples as a base. They have thought about lots of things like area codes in brackets, spaces or dashes between groups of digits, etc. You will need to think about what you want to support:
your local conventions / rules for numbers
domestic area codes
international numbers and dialling prefix
and so on.

It's not as simple as some people think.

Peter

[edit] Here's a simple example of using a Javascript regexp
JavaScript
var pattern = /^\(\d{3}\)\s*\d{3}(?:-|\s*)\d{4}$/
if (pattern.test(string)) {
    // string looks like a good (US) phone number with optional area code, space or dash in the middle
}
[/edit]
 
Share this answer
 
v2
JavaScript
function Validate()
        {
            var x = document.form1.txtPhone.value;
            var y = document.form1.txtMobile.value;
           if(isNaN(x)||x.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (x.length>8)
           {
                alert("enter 8 characters");
                return false;
           }
           if (x.charAt(0)!="2")
           {
                alert("it should start with 2 ");
                return false
           }

           if(isNaN(y)||y.indexOf(" ")!=-1)
           {
              alert("Enter numeric value")
              return false; 
           }
           if (y.length>10)
           {
                alert("enter 10 characters");
                return false;
           }
           if (y.charAt(0)!="9")
           {
                alert("it should start with 9 ");
                return false
           }
        }
 
Share this answer
 
hi,,


XML
<script type="text/javascript">
    function onlyNumbers(event) {
        var e = event || evt; // for trans-browser compatibility
        var charCode = e.which || e.keyCode;
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
</script>




this will allow only numbers in text
 
Share this answer
 
<
<div class="input-field col s6">
                    <label>
                      Phone Number
                    </label>
                    <input type="text" name="phone" class="phone required">
                  </div>

<script type="text/javascript">
        var $el = {
          phone: $('[name=phone]'),
        }
        var regex = {
  phone: {
    sg: /^(\d{4})(\d{4})$/,
    us: /^1?(\d{3})(\d{3})(\d{4})$/,
  }
};

// Custom phone number validation
function validatePhone(value, el) {
  // Get country code
  var cc = $el.country.val();
  // Normalize value
  value = $.trim(value).replace(/\D/g, '');
  // Validate!
  var re = regex.phone[cc],
    isOptional = this.optional(el),
    isValid = value.length > 7 && re.test(value);
  if (isValid) {
    switch (cc) {
      case 'sg':
        // Output nicely formatted Singapore phone number
        el.value = value.replace(re, '$1-$2');
        break;
      case 'us':
        // Output nicely formatted US phone number
        el.value = value.replace(re, '($1) $2-$3');
        break;
      default:
        el.value = value;
    }
  }
  return isOptional || isValid;
}

// Add custom validator method for inputs with "phone" class
$.validator.addMethod('phone', validatePhone, 'Invalid phone number.');

// Validate form
$el.form.validate({
  onkeyup: false
});

$el.country.change(function() {
  // Re-validate phone number
  $el.phone.valid();
  // Or re-validate entire form
  //$el.form.valid();
});

      </script>
 
Share this answer
 
For Phone number and mobile number validation :
function PhoneNumberValidation() {

    var txtPhone = document.getElementById("txtPhoneNo").value;
    var phoneno = /^\+?([0-9]{2})\)?[-. ]?([0-9]{4})[-. ]?([0-9]{4})$/;
    var mobileno = /^((\\+91-?)|0)?[0-9]{10}$/;
    if ((txtPhone.match(phoneno)) || (txtPhone.match(mobileno))) {
        document.getElementById("demo").innerHTML = "Valid";
    }
    else {
        document.getElementById("demo").innerHTML = "Not Valid";
    }

}
 
Share this answer
 
Comments
Maciej Los 20-Mar-20 3:21am    
Do you think that questioner is interested in an answer after 9 years?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900