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!

Link to comment
Share on other sites

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.');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.