0

I am trying to add validation to an email field using JavaScript and have attempted to use the following pattern:

pattern="[a-z0-9]\.!#$%&'*+/=?^_`{|}~-]+@[a-z-]+(\.[a-z]{2,})+"

This works fine in several browsers but not Chrome, where I always get some variation of the following error:

Pattern attribute value [a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z-]+(.[a-z]{2,})+ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z-]+(.[a-z]{2,})+/v: Invalid character in character class

I've tried a lot of variations, including escaping several characters or removing them entirely, with the same results. I'm assuming this has something to do with the /v flag, but I have no idea how to fix it. Has anyone else encountered this?

  • Escape literal `-`, `(`, `)`, `[`, `{`, `}`, `/` and `|` symbols in character classes. I.e. ``pattern="[a-z0-9\].!#$%&'*+\/=?^_`\{\|\}~\-]+@[a-z\-]+(?:\.[a-z]{2,})+"`` – Wiktor Stribiżew Jul 20 '23 at 22:48

0 Answers0