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? :)
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? :)
Share Improve this question edited Mar 26, 2016 at 12:42 phen0menon asked Mar 26, 2016 at 12:32 phen0menonphen0menon 2,4522 gold badges19 silver badges37 bronze badges 4-
1
d
matchesd
alphabet. You want\d
to match digits.)
need to escape to match closing brace – Tushar Commented Mar 26, 2016 at 12:33 - Also, it will only match a string of 11 digits – gurvinder372 Commented Mar 26, 2016 at 12:36
- @gurvinder372 sry i missed. +#-###-###-##-## - that's right – phen0menon Commented Mar 26, 2016 at 12:40
-
It seems to me,
/^\+(?:\d(?:\(\d{3}\)|-\d{3})-\d{3}-(?:\d{2}-\d{2}|\d{4})|\d{11})$/
should work for you best. – Wiktor Stribiżew Commented Mar 26, 2016 at 12:53
3 Answers
Reset to default 3You 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
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
As Tushar has pointed out,
d
will match onlyd
, to match a digit you need\d
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
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;
}