Scooby08 Posted January 26, 2012 Share Posted January 26, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/255823-regex-for-us-phone-number-with-all-the-same-digits/ Share on other sites More sharing options...
abareplace Posted January 26, 2012 Share Posted January 26, 2012 Try ... && !phone_number.match(/^(?:1-?)?(\d)\1\1-?\1\1\1-?\1\1\1\1$/) Quote Link to comment https://forums.phpfreaks.com/topic/255823-regex-for-us-phone-number-with-all-the-same-digits/#findComment-1311405 Share on other sites More sharing options...
Scooby08 Posted January 26, 2012 Author Share Posted January 26, 2012 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.'); Quote Link to comment https://forums.phpfreaks.com/topic/255823-regex-for-us-phone-number-with-all-the-same-digits/#findComment-1311408 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.