Click here to Skip to main content
14,327,173 members
Rate this:
Please Sign up or sign in to vote.
See more:
how to validate phone and mobile number in javascript.
for example phone no -044 27221343. mobile no-9944034466/8428664422
/7666544221
Posted
Updated 18-Apr-19 23:26pm
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
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]
   
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

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
           }
        }
   
Rate this:
Please Sign up or sign in to vote.

Solution 8

<
<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>
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

hi,,


<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
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100