1

Below is my current code. I need to make sure it always has the full phone number of Ten digits. Does anyone have the proper regex code? "US numbers"

"phone": {
    // credit: jquery.h5validate.js / orefalo
    "regex": /^([\+][0-9]{1,3}[ \.\-])?([\(]{1}[0-9]{2,6}[\)])?([0-9 \.\-\/]{3,20})((x|ext|extension)[ ]?[0-9]{1,4})?$/,
    "alertText": "* Invalid phone number"
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Brian Martin
  • 35
  • 2
  • 5

2 Answers2

1
  <script>
  $(document).ready(function(){
    jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
    return this.optional(element) || phone_number.length > 9 &&
        phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

$("#myform").validate({
  rules: {
    field: {
      required: true,
      phoneUS: true
    }
  }
});
  });
  </script>
Pushpendra
  • 548
  • 3
  • 10
1

Please see: A comprehensive regex for phone number validation for more comprehensive answers. But, I think that this is the Regex that you want:

^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$
Community
  • 1
  • 1
A.M.K
  • 16,727
  • 4
  • 32
  • 64
  • it will accept 1 number only. So as long as when someone fills out the form and puts in a single digit it allows them to move forward. I want to make sure they put in the full 10 digit US phone number. – Brian Martin Sep 16 '12 at 20:46