Jquery Validate Phone Number With With Regex

jQuery validate phone number with with RegEx

Your regex works fine for me... you could shorten it to just /[0-9]{10}/.

Your problem is here:

$("#call_number").val().length < 1. If the number is 10 characters long, it will never be less than 1, no?

You probably meant something like this:

$("#call_number").val().length === 10

jQuery Phone Regex Validation

You need the following regex:

/^\+(?:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})|\d{11})$/

See the regex demo

The regex you have ^\+d{1}(\d{3})\d{7}$ has d instead of \d (thus failing to match digits) and unescaped parentheses (thus the pattern did not match literal parentheses).

Breakdown:

  • ^ - start of string
  • \+ - a literal + symbol
  • (?:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})|\d{11}) - two alternatives:

    • \d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4}):

      • \d - a digit
      • (?:\(\d{3}\)|-\d{3}) - either (123) like substring or -123 substring
      • -\d{3} - a hyphen followed with 3 digits
      • - - a hyphen
      • (?:\d{2}-\d{2}|\d{4}) - 2 digits followed with a hyphen and 2 digits or 4 digits
    • | - or
    • \d{11} - 11 digits
  • $ - end of string

Example of a regular expression for phone numbers

Use this rexeg

/\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/
  • (123) 456 7899
  • (123).456.7899
  • (123)-456-7899
  • 123-456-7899
  • 123 456 7899
  • 1234567899

supported

Jquery form validation - telephone number

I would highly recommend using jQuery validate rather than re-inventing the wheel

a quick google search for uk numbers with jquery validate turned this up

filters

that looks like this

jQuery.validator.addMethod('phoneUK', function(phone_number, element) {
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/^(\(?(0|\+44)[1-9]{1}\d{1,4}?\)?\s?\d{3,4}\s?\d{3,4})$/);
}, 'Please specify a valid phone number'
);

Modify given regex to check if string contains no phone number

Following the logic from our chat, here is the pattern:

/^(?!.*(?:\s|^)(?:\+\d|0)(?:[()-]*\d){6,}(?!\S))/

See the regex demo.

Details:

  • ^ - start of string
  • (?! - start of a negative lookahead:
    • .* - any zero or more chars other than line break chars, as many as possible
    • (?:\s|^) - whitespace or start of string
    • (?:\+\d|0) - + and a digit, or a zero
    • (?:[()-]*\d){6,} - six or more occurrences of zero or more (, ) or - chars and then a digit
    • (?!\S) - no non-whitespace char allowed immediately to the right of the current location
  • ) - end of the lookahead check.

regex to validate phone number jquery

Check out this solution using negative lookahead:

^(?![8]{10}|[9]{10})[7-9]{1}[0-9]{9}$

jquery validate phone number and extension

// custom extension regex
phone: function(value, element) {
return this.optional(element) || /^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})\s*-?[2-9]\d{2}-?\d{4}(\s*x\s*\d{4})?$/.test(value);
},


Related Topics



Leave a reply



Submit