Reputation: 59
i wrote the following regular expression to determine if a national or international telephone number has the right pattern.
/^(00|0|[+])([-][0-9]|[0-9]){1,13}[0-9]/
The Pattern could be improved by splitting the numbers into countrycode and phone number but this does not matter yet.
Thanks for your help
edit: here´s the code. It should not be the problem because other regex work well. oChanged is a textinput.
function verification(oChanged, regex){
var val = $(oChanged).val();
var reg = new RegExp(regex);
if( !reg.test(val) ){
if( !$(oChanged).hasClass('notvalid') ){
$(oChanged).addClass('notvalid');
}
}else{
if( $(oChanged).hasClass('notvalid') ){
$(oChanged).removeClass('notvalid');
}
}
}
I tested it with some regex testers in the internet and it matched all these strings but when I implement it it does not match.
it should work for e.g. 004915773554660 +4915773554660 015773554660 0049157-7355-466-0
Upvotes: 0
Views: 200
Reputation: 44
Please use this one:
/(+\d{1,3}\s?)?(((\d{3})\s?)|(\d{3})(\s|-?))(\d{3}(\s|-?))(\d{4})(\s?(([E|e]xt[:|.|]?)|x|X)(\s?\d+))?/g
Demo: https://www.regextester.com/103299
Full number
+999 (999) 999-9999 Ext. 99999
Regular local phone number (XXX) XXX-XXXX
1231231231 123-1231231 123123-1231 123-123 1231 123 123-1231 123-123-1231 (123)123-1231 (123)123 1231 (123) 123-1231 (123) 123 1231
International codes +XXX (XXX) XXX-XXXX
+99 1234567890 +991234567890
Extensions (XXX) XXX-XXXX Ext. XXX...
1234567890 Ext 1123123 1234567890Ext 1123123 1234567890 Ext1123123 1234567890Ext1123123
Upvotes: 1