0

I am trying to write in java script to exclude invalid mobile numbers that are entered into the SQL database. i currently have the regex function within my script however it cant pick up brackets(), resulting in numbers such as (123) 456789 not being included.

Is there any extension to the regex i could use to include brackets?

user3207341
  • 69
  • 1
  • 2
  • 9

2 Answers2

1

There is a free google API that can validate numbers for you, even tell you the country code etc. https://code.google.com/p/libphonenumber/

Never tried it, but I evaluated it once for another project I worked on.

This is Java, not JS, but still you may consider moving your validation logic to a Java servlet and invoke using an AJAX call.

RuntimeException
  • 1,593
  • 2
  • 22
  • 31
0

1) This would expect atleast one white space character after parenthesis

/\(\d{3}\)\s+\d{6}/

Or

/\(\d+\)\s+\d+/ //in case of digit not specified.

2) This would match with or without space

/\(\d{3}\)\s*\d{6}/

//eg:-
// (123)456789
// (123) 456789
Suman Bogati
  • 6,289
  • 1
  • 23
  • 34
  • 1
    There can be more digits than 6. china has 11 digit numbers. If user enters china country code plus 12 digits it is invalid. For UK, there are ten digits after country code. It becomes complicated if you want to support phone numbers from around the world. Better leave things like Daylight savings, phone numbers, etc to existing libraries. – RuntimeException Mar 04 '14 at 14:14
  • \d+ this would match 1 or more. It does not matter. – Suman Bogati Mar 04 '14 at 14:18
  • @downvoter, Why downvote can you please explain? – Suman Bogati Mar 04 '14 at 14:22
  • that would match 1 or more, but there is no one digit phone number that I know of. A number like +9198 is still invalid. Well it depends on the requirements of the OP, really. Your solution will work if the OP wants to just check if it meets the pattern, even if user inputs three zeros in parantheses followed by six or seven zeros. – RuntimeException Mar 04 '14 at 14:22
  • As question asked by OP, I gave the answer. – Suman Bogati Mar 04 '14 at 14:28
  • In a way, you are right, Suman. BTW, though there are only two of us commenting here, I did not down vote. – RuntimeException Mar 04 '14 at 14:48