0

I have a phone number input field where it must:

  1. Allows spaces and "-" and "+" characters.
  2. Have 12 characters max including the special characters (ex +12345678910).

I tried but my best result was ng-pattern="/^\+?\d{0,11}?$/".
How to make this work?

Anna
  • 839
  • 2
  • 17
  • 33

1 Answers1

1

Seems like you're trying to match a phone number? Maybe this is helpful: What regular expression will match valid international phone numbers?

Otherwise, if no particular order or restrictions for your numbers, +, - or spaces, this should do the work: /^[0-9\+\-\s]{1,12}$/

https://regex101.com/r/kuc5OF/2

Community
  • 1
  • 1
Andrei Duca
  • 372
  • 2
  • 11
  • Thanks for the answer but how to limit the length to 12 characters? – Anna Apr 25 '17 at 13:15
  • Do you want to limit the length of ALL the characters, including the special ones `+`, `-` and spaces? That would be the `{1,12}` part. – Andrei Duca Apr 25 '17 at 13:18
  • Yeah including the special ones. I edited my question. This doesn't seem to work --> [Test](https://regex101.com/r/kuc5OF/1/) – Anna Apr 25 '17 at 13:21
  • `^/[0-9\+\-\s]{1,12}/$` - you need to specify the start `^` and end `$` modifiers for an exact match. I updated my answer. – Andrei Duca Apr 25 '17 at 13:22
  • You're welcome! As long as it worked, no time was wasted. – Andrei Duca Apr 25 '17 at 13:28