Reputation: 113
I want to validate multiple phone numbers comma separated using regular expression in javascript.
For single number my regex is /03[0-9]{9}$/
i need to test this : 03123465789,03335678990,03453465789
Thanks in Advance.
Upvotes: 0
Views: 1255
Reputation: 9650
If you need to check if a string ends with a series of phone numbers of a given pattern separated by comma, try this:
(?:03[0-9]{9}(?:,|$))+$
Demo: https://regex101.com/r/dE1wD2/1
Explanation:
The (?:,|$)
ensures a phone number of your desired pattern either ends with comma or appears to be the last number in the string. The final "$" is put to ensure no other stuff comes after the numbers.
Update:
Just to be more precise and exclude lists ending with a comma, append a look-ahead "not an end of line" condition to the comma (i.e. ,(?!$)
):
(?:03[0-9]{9}(?:,(?!$)|$))+$
Demo: https://regex101.com/r/dE1wD2/2
Upvotes: 2