0

using React, I am supposed to build a form, I was given a pattern I have add to input :"+?\d{1,4}?[-.\s]?(?\d{1,3}?)?[-. \s]?\d{1,4}[-.\s]?\d{1,4}[-.\s]?\d{1,9}" ( phone number validation) but when I enter the phone number in the form, I get error: Pattern attribute value +?\d{1,4}?[-.\s]?(?\d{1,3}?)?[-.\s]?\d{1,4 }[-.\s]?\d{1,4}[-.\s]?\d{1,9} is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /+?\d{ 1,4}?[-.\s]?(?\d{1,3}?)?[-.\s]?\d{1,4}[-.\s]?\d{ 1,4}[-.\s]?\d{1,9}/v: Invalid character in character class.

I don't know how to correct the regex. Could someone help me?

I added anchors ^ and $ did not help

"Phone number must be digits and can contain spaces, dashes, parentheses and can start with +"

  • Escape the literal hyphen inside character classes. Adding `^` and `$` is meaningless in this context (they are added automatically). – Wiktor Stribiżew Jul 10 '23 at 10:19
  • 1
    @Wiktor Stribiżew thank you very much :) Your one sentence was enough to solved problem – Izabela Świadkowska Jul 10 '23 at 11:14
  • Yes, but [the linked answer](https://stackoverflow.com/a/76287241/3832970) explains the root cause, that is quite a recent thing, that will startle a lot of people in the nearest future. At least until this is fixed (or if it is fixed). – Wiktor Stribiżew Jul 10 '23 at 11:18

0 Answers0