Reputation: 2401
I am trying to validate Phone Numbers that follow the next Pattern
01\d{9}
2\d{7}
But The user can enter more than one number separated by space or in one input field So I come up with the regular expression
/(?:(?:01\d{9}$)|(?:2\d{7}$) ){1,}
A Test Sample
"01226113130 26322612 24586154 01004598654"
My Expression Doesn't match this sample, any help ?
Solution For others if they fail in the problem, You can try Jerry Solution or this one
(?:(?:(?:01\d{9}(?:[\- \,])*)|(?:2\d{7}[\- \,]*))){1,}
Upvotes: 2
Views: 4921
Reputation: 71578
Try this one:
^(?:(?:01\d{9}|2\d{7}) ){1,}(?:01\d{9}|2\d{7})$
Your current regex has (?:01\d{9}$)|(?:2\d{7}$)
where the $
forced it to 'prematurely end' the match, so removing this was the first thing to do. Then (?:01\d{9})|(?:2\d{7})
can be re-written as (?:01\d{9}|2\d{7})
. I added a ^
for the beginning of the string.
Afterwards, this regex will only validate strings ending with a space, so add another (?:01\d{9}|2\d{7})
at the end and finally conclude with $
.
Oh, also, it might be better to turn the {1,}
into *
like this:
^(?:(?:01\d{9}|2\d{7}) )*(?:01\d{9}|2\d{7})$
Upvotes: 2