0

I have regex which allow these format

Regex:

/^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}$/

Format:

(123) 456-7890
123-456-7890
123.456.7890
1234567890

I want to make it such that it allows 7 digits or more than 7 digits. Currently it allows 10 digits not less or more than that. Please help!

Wiseguy
  • 20,522
  • 8
  • 65
  • 81
Ammar Khan
  • 346
  • 1
  • 9
  • 27
  • 1
    Are you in UK, France, .... But I guess it is the US as the rest of the world does not matter – Ed Heal Jan 24 '14 at 23:14

3 Answers3

1

Don't bother with difficult phone numbers like this - if you simply want to check, why not go the simple route and replace anything that isn't a digit, and then check the length of the string?

var telephoneNumber = '(123) 456-7890';

telephoneNumber = telephoneNumber.replace(/[^\d]+/g, '');
if (telephoneNumber.length >= 7) {
    console.log('Everything is OK! The number is: ' + telephoneNumber);
    //Everything is OK! The number is: 1234567890
} else {
    console.log('Something went wrong!');
}

This would also match 123-abc-456-7890, but would "understand it" correctly.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • I don't want to allow a-z, space with no would be fine. And I can enter 7 digits and more. Most importantly I need a regular expression for this instead of function. – Ammar Khan Jan 24 '14 at 22:55
  • @user2866746 `if (telephoneNumber.replace(/[^\d]+/g, '').length >= 7)` is no function - is one line - and works. – h2ooooooo Jan 24 '14 at 23:01
  • actually I am using jQuery live validation plugin and it only accept pattern to match. That's why I insist to have regex for this. – Ammar Khan Jan 24 '14 at 23:04
  • @user2866746 Technically, if this rule is okay, you could just go with something as silly as `^(.*\d){7,}.*$`: [**DEMO**](http://regex101.com/r/hI9cF5) – h2ooooooo Jan 24 '14 at 23:10
  • Alternately, if it's max 10 digits: `^(.*\d){7,10}.*$` works fine. – h2ooooooo Jan 24 '14 at 23:12
0

try this:

^[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{1,5}$

It will check that you have at least 7 numbers (and max 11). How many numbers do you want to allow?

i-bob
  • 423
  • 2
  • 5
0

There is no such thing as a regex to match phone numbers for multiple countries, which is what it sounds like you need. Trust me, I've tried.

And as stated above, just allow a user to input anything and strip anything which isn't a number before saving it to a database. This way, when you redesign your application in 2 years and the designer says "I want decimals instead of dashes", you don't have to go and muck with the database - you just adjust your presentation layer.

Something like this might suit you though:

/^(?:[(]{0,1}[0-9]{3}[)]{0,1}[-\s\.]{0,1}[0-9]{3}[-\s\.]{0,1}[0-9]{4}|[0-9]{3}[-\s\.]{0,1}[0-9]{4})$/

Matches the following:

1234567
123 4567
123.4567
123-1234
1234567890
123 456 7890
123.456.7890
123-456-7890

...but not this:

(123)1234

Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96