Jump to content

Regex for US Phone Number with all the same digits


Scooby08

Recommended Posts

I'm using the following code to validate US Phone Numbers:

 

jQuery.validator.addMethod('phoneUS', function(phone_number, element){
phone_number = phone_number.replace(/\s+/g, ''); 
return this.optional(element) || phone_number.length > 9 &&
	phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, 'Please specify a valid phone number.');

 

I would like to add a check to not allow phone numbers like 222-222-2222 that have all the same digits.. I think it would be something like below, but that could check all numbers rather than just 222-222-2222..

 

phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/) && !phone_number.match(/^222-222-2222$/);

 

So I need a regex like this but for all digits..

 

^222-222-2222$

 

Thanks!

That worked perfectly!

 

Thanks abareplace!

 

jQuery.validator.addMethod('phoneUS', function(phone_number, element){
phone_number = phone_number.replace(/\s+/g, ''); 
return this.optional(element) || phone_number.length > 9 &&
	phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/) && 
	!phone_number.match(/^(?:1-?)?(\d)\1\1-?\1\1\1-?\1\1\1\1$/);
}, 'Please specify a valid phone number.');

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.