Reputation: 8800
Can Anyone give me regex for validate number which is
(),-,#(
if there then only at first place followed by number),/,+eg.
(079) 22861851
(079)22861851
079 22861851
22861851
079-26408300 / 8200
079 264 083 00
9429527462
+919427957462
#9427957462
i want it all above number to validate true withing one regex formula..can anyone help me.?
i have tried this
var phone_patternIndia = /^((\+){0,1}91(\s){0,1}(\-){0,1}(\s){0,1}){0,1}\d{2}(\s){0,1}(\-){0,1}(\s){0,1}[1-9]{1}[0-9]{7}$/;
var phone_patternUsa=/^([0-9]( |-)?)?(\(?[0-9]{3}\)?|[0-9]{3})( |-)?([0-9]{3}( |-)?[0-9]{4}|[a-zA-Z0-9]{7})$/;
var phone_patternUsa1=/^[\\(]{0,1}([0-9]){3}[\\)]{0,1}[ ]?([^0-1]){1}([0-9]){2}[ ]?[-]?[ ]?([0-9]){4}[ ]*((x){0,1}([0-9]){1,5}){0,1}$/;
but its not working all time ..so decide to go with other pattern..
var phone_pattern=/^((\d{3}-?|(\d{3}))\s*\d{7}($|\s*/\s*\d{4}$)|\d{3}\s\d{3}\s\d{3}\s\d{2}|+\d{12}|#\d{10})$/;
if (phone_pattern.test(personal_phone))
{
$("#restErrorpersonalphone").html('');
$("#personal_phone").removeClass('borderColor');
} else {
$("#restErrorpersonalphone").html('Please enter valid phone number');
$("#personal_phone").addClass('borderColor');
flag = false;
}
its always going in else condition
Upvotes: 0
Views: 204
Reputation: 10250
Your easiest approach to the problem would be to eliminate all non-digits and validate using the length of only the digits.
var digits=personal_phone.replace(/[^0-9]/g,'');
var isValid=false;
if (digits && digits.length>=10) {
// You can fine-tune whether it starts with 1, matches an area code, etc here
isValid=true;
}
Upvotes: 1
Reputation: 3421
I strongly encourage breaking this into smaller regex and testing each individually, or else stripping out chars, then testing, but just a rough sample off top of my head that should help with your question ( not tested and only covers the examples you listed )...
(079) 22861851 => /^(\d{3}-?|\(\d{3}\))?\s*\d{7}(\s*\/\s*\d{4})?$/
(079)22861851 => same...
079 22861851 => same...
22861851 => same...
079-26408300 / 8200 => same...
079 264 083 00 => /^\d{3}\s\d{3}\s\d{3}\s\d{2}$/
9429527462 => same as first regex
+919427957462 => \^\+\d{12}$/
#9427957462 => /^#\d{10}$/
So, just combine them...
/^(\d{3}-?|\(\d{3}\))?\s*\d{7}($|\s*\/\s*\d{4}$)|\d{3}\s\d{3}\s\d{3}\s\d{2}|\+\d{12}|#\d{10})$/
Upvotes: 2