admin管理员组

文章数量:1431391

I am trying to generate a regex which would match the following sequence-

+91123456789,+41123456789,+21123456789.... and so on, there is no limit of phone numbers.

Basically the usage is to validate the phone numbers which users may add, phone number can be multiple and need to be separated by mas, I am already removing the empty spaces which users may add, so no worry for that.

I am not good with regex and have created the following regex but it doesn't matches the preceding phone numbers, means the whole string of phone numbers do not match-

^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9},\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$

I need to validate the user input using javascript or jquery.

Valid Phone number should be having country code like +91 or +21 etc country code can be of one or two digits, then the number of digits need to be 7 to 9.

I anyone could help, it would be highly appreciable, I have spent lot of time on this one.

I am trying to generate a regex which would match the following sequence-

+91123456789,+41123456789,+21123456789.... and so on, there is no limit of phone numbers.

Basically the usage is to validate the phone numbers which users may add, phone number can be multiple and need to be separated by mas, I am already removing the empty spaces which users may add, so no worry for that.

I am not good with regex and have created the following regex but it doesn't matches the preceding phone numbers, means the whole string of phone numbers do not match-

^\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9},\+?\d{1,4}?[-.\s]?\(?\d{1,3}?\)?[-.\s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}$

I need to validate the user input using javascript or jquery.

Valid Phone number should be having country code like +91 or +21 etc country code can be of one or two digits, then the number of digits need to be 7 to 9.

I anyone could help, it would be highly appreciable, I have spent lot of time on this one.

Share Improve this question edited Sep 24, 2015 at 11:06 Manik Arora asked Sep 24, 2015 at 10:58 Manik AroraManik Arora 4,8021 gold badge26 silver badges48 bronze badges 4
  • 1 I find www.regex101. to be a great resource. You haven't specified in your question what you consider a valid phone number to be, what makes a valid number? – Jack Allan Commented Sep 24, 2015 at 11:01
  • Why not just \+\d{11}/g? – Johan Commented Sep 24, 2015 at 11:01
  • @jack, you're right, I have been testing the specified regex on regex101., I am adding the format of valid phone number in question. thanks – Manik Arora Commented Sep 24, 2015 at 11:02
  • 1 Perhaps split on , and examine each individually, that way you can easily tell the the user the specific one that needs correction. – Alex K. Commented Sep 24, 2015 at 11:26
Add a ment  | 

2 Answers 2

Reset to default 4

To validate the whole string handling mulitple values sepparated by ma just add an group with * multiplier:

^\+\d{8,11}(,\+\d{8,11})*$

If I understand the requirements correctly, the following regex should work

\+\d{9,11}

However, you can separate the country code out, for if you need to allow for (+44)xxxxxxxxx

\+\d{2}\d{7,9}

if the requirement is to allow for 1 country code as well, adjust the regex to the following

 \+\d{1,2}\d{7,10} //I think to 10, not sure on their numbers

You can update the ranges as you see fit :)

Demo: https://regex101./r/rJ4wM7/1

本文标签: javascriptRegex Comma Separated Phone NumberStack Overflow