phen0menon
phen0menon

Reputation: 2452

jQuery Phone Regex Validation

I have jQuery Validation plugin on a page. When someone types the phone number into the form field, I want the validator to only recognize a certain format (ru):

+#(###)-###-##-##
or
+#-###-###-####
or
+#-###-###-##-##
or
+###########

I have this in .js file:

$.validator.addMethod('customphone', function (value, element) {
    return this.optional(element) || /^\+d{1}(\d{3})\d{7}$/.test(value);
}, "Please enter a valid phone number");

$(document).ready(function() {
    $('form').validate({    
        rules: {
            phone: 'customphone'
        } ...

This is not working for me, does anyone see why? Or is there a better way to do this? :)

Upvotes: 2

Views: 6998

Answers (3)

Wojo
Wojo

Reputation: 65

I am not sure about your javaScript but here is the different regex you have to check for.... I hope this helps.

+#(###)-###-##-##
^\+\d{1}\(\d{3}\)\-\d{3}\-\d{2}\-\d{2}$
+#-###-###-####
^\+\d{1}\-\d{3}\-\d{3}\-\d{4}$
+#-###-##-##
^\+\d{1}\-\d{3}\-\d{2}\-\d{2}$
+###########
^\+\d{11}$

So I would setup a phone is valid flag and test each regex value to determine if one of them is true set this flag to true and it would validate your phone number.

var phoneValid = false;

if(test1 == true || test2 == true || test3 == true || test4 == true) {
  phoneValid = true;
} else {
  phoneValid = false;
}

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

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

Upvotes: 3

gurvinder372
gurvinder372

Reputation: 68363

This is not working for me, does anyone see why? Or is there a better way to do this? :)

There are a couple of issues with your code

  1. As Tushar has pointed out, d will match only d, to match a digit you need \d

  2. Your regex is not affording for many things including (###) and -

I guess you are looking for this regex

/^\+\d(\(\d{3}\)){0,1}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}$/g

It will match for

/^\+\d(\(\d{3}\)){0,1}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}$/g.test("+4(222)-33-33-33"); //true
/^\+\d(\(\d{3}\)){0,1}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}$/g.test("+4(222)-333-333-333"); //true
/^\+\d(\(\d{3}\)){0,1}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}$/g.test("+4(222)333333333"); //true
/^\+\d(\(\d{3}\)){0,1}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}(\-){0,1}\d{2,3}$/g.test("+4333333333"); //true

Upvotes: 1

Related Questions