0

The format I need for this form is (360)555-5555 (no space, single dash, with parenthesis). I looked around on here already but didn't see an answer to this specific set up. Not sure why it isn't working with the regex below.

var phonepattern = /^\\(\\d{3}\\)\\d{3}[-]\\d{4}$/;
  • I'm guessing it's because you double escaped the `\d` – adeneo Aug 12 '16 at 23:01
  • A literal ``\`` should escape a shorthand character class, and you escaped the literal backslash thus matching a ``\`` and then a letter `d` with `/\\d/`. Replace ``\\`` with ``\``. – Wiktor Stribiżew Aug 12 '16 at 23:02
  • What would that look like? – Amy Gaston Aug 12 '16 at 23:05
  • ^\\(\d{3}\\)\d{3}[-]\d{4}$ – Cyferka Aug 12 '16 at 23:05
  • `^\(\d{3}\)\d{3}\-\d{4}$` – Hasan Can Saral Aug 12 '16 at 23:10
  • Seriously @Amy, **do *not*** force people to type in _exactly_ that pattern and no other. If that is how you ultimately want the phone number you can (and should) still accept other things the user types, and then turn it into that format. Let people type just 10 digits `nnnnnnnnnn` or with all dashes `nnn-nnn-nnnn` or spaces, or periods because `nnn.nnn.nnnn` is super-easy to type on the numeric keypad. Validation: strip out non-digits `phone.replace(/\D/g, '')` make sure it's 10 digits `.match(/^\d{10}$/) !== null` then take those 10 digits and reformat it yourself into `(nnn)nnn-nnnn` – Stephen P Aug 13 '16 at 00:45

0 Answers0