2

I am working on a chat application where user have to fill the form before he/she begins the chat. Validation of each form field happens once user enters the value in respective form field. One of the form field is phone number and for which I wrote a regular expression as following.

var phoneRegex = RegExp(/^[\+]?[(]?[0-9]{3}[)]?[-\s\.]?[0-9]{3}[-\s\.]?[0-9]{4,10}$/im);

Regex should return 'true' for following phone numbers

  • +11234567890 - 10 digits with area code
  • 1234567890 - 10 digits
  • 233445678912 - 12 digits
  • 2334456789122222 - 16 digits

My chat application works for first 3 numbers but doesn't work for 16 digit phone number. I am not a huge fan of regex and have limited understanding of it hence I want to to know if my regex has any fault? And if yes then how can I fix it?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Sid
  • 122
  • 6
  • 1
    Are you saying it should work for those numbers in addition to handling things like parentheses and dashes? Can you clarify your requirements a bit? – Slbox Apr 22 '21 at 23:50
  • Consider a general purpose library: https://github.com/google/libphonenumber – Jevon Kendon Apr 22 '21 at 23:51
  • Strongly recommend _against_ that library. It's enormous for web applications and tree-shaking does little if anything to help. It's functionally the best though. – Slbox Apr 22 '21 at 23:52
  • 1
    Thanks for the advice @Slbox. Admittedly I've only used the C# port in the backend. – Jevon Kendon Apr 22 '21 at 23:55
  • 2
    Validation should be done server-side anyway and libphonenumber is fine for server apps. If you must do client side validation, your regex will pass on faulty input like `+(123456789`. It may be as to have a sanity check like `/^\+?[\d\(\)\s-]*$` that only allows digits, parentheses and dashes, but does not try to enforce formatting. The reason for this is all countries have differently formatted phone numbers. Trying to write a matcher for all of them is a fool's errand. – Charlie Bamford Apr 23 '21 at 00:00
  • @Slbox parentheses and dashes are excluded from the requirements so the sample/test phone numbers that I proved in my question portrays all the requirements. – Sid Apr 23 '21 at 00:01
  • If you only want to match those four cases, `^(\+\d)?\d{10}(\d{2})?(\d{4})?$` Would be sufficient. – Charlie Bamford Apr 23 '21 at 00:05
  • 2
    Does this answer your question? [How to validate phone numbers using regex](https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex) and there's also this... https://stackoverflow.com/questions/4338267/validate-phone-number-with-javascript – Mick Apr 23 '21 at 00:28
  • 1
    "in my question portrays all the requirements" — is the initial `+` allowed _only_ on the Ten digit number, not allowed on the Twelve or Sixteen digit numbers? Whatever the answer is, that is an _unstated_ requirement. It is unclear as currently stated, which Slbox mentions in her/his Answer below. – Stephen P Apr 23 '21 at 01:02
  • The questions this is marked as a duplicate of are not very "duplicate" at all - not to mention the accepted answer for one of them is to use a gigantic library that we've discussed in these comments, and that I've recommended against for most use cases, *especially front-end, which this seems to be.* OP had specific requirements that differ from those questions and I feel this should be re-opened. – Slbox Apr 23 '21 at 18:01

2 Answers2

0

How about this?

It checks for the optional leading '+', followed by a 16 digit number, and if that fails, it checks for any of a 10, 11, or 12 digit number.

One thing I'm not entirely clear on is if that plus is always optional though. Also I think you might mean country code instead of area code? If I'm wrong about that, then this regex is no good.

^\+?(\d{16}|\d{10,12})

Results on Regexr (without the leading ^, for example purposes)

enter image description here


Alternatively you can try this

It's a bit more strict about things

^(\d{16}|\+?\d{11}$|\d{10,12})
Slbox
  • 10,957
  • 15
  • 54
  • 106
0

Since your phone field is a continuous string of digits (or at least that’s what I understood from your post) why do you need to use RegExp?

Just use basic logic, something like this...

var N='1234567890';

var Valid=((isNaN(N)===false)&&(Number(N)>1000000000)&&(Number(N)<9999999999999999));

alert(Valid);  // test it.
jsuser
  • 24
  • 2
  • This might be easier to understand if you don't know Regex, but this seems like madness to me. – Slbox Apr 23 '21 at 00:39
  • 2
    A String of Digits is _not_ a number which you can compare with `<` and `>` -- Phone numbers in some places are allowed to have leading zeros e.g. "0001239876" which would fail the `>1000000000` test if treated as a Number, but is (could be) valid as a _phone_-number. – Stephen P Apr 23 '21 at 00:58