Reputation: 5635
I'm trying to create regex match for phone number in jquery, for exactly this format
+XXX XXX XXX XXX, where X is digit.
I created this
var regEx = '[+]{1}[\d]{3}[ ]{1}[\d]{3}[ ]{1}[\d]{3}[ ]{1}[\d]{3}';
var val = jQuery.trim($('#phone_number').val())
if (val.match(regEx)) {
alert('good');
}
else
alert('bad');
}
Any assistance will be helpful.
Upvotes: 1
Views: 673
Reputation: 5268
You almost have it, but there are a few "unnecessaries", and you're missing anchors assuming the phone number is not part of a larger string (and this is for validation).
^\+\d{3} \d{3} \d{3} \d{3}$
Upvotes: 1