-1

How can I make this regular expression work?

To validate emails, my regular expression is supposed to be fine, but I use it in the html pattern and I see in the console that it throws me this error: email.php:1 Pattern attribute value ^(?!.*([.-])\1)(?!.*([.-])$)(?!.*[.-]$)(?!.*[.-]{2})[a-zA-Z0-9_%+-][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ is not a valid regular expression: Uncaught SyntaxError: Invalid regular expression: /^(?!.*([.-])\1)(?!.*([.-])$)(?!.*[.-]$)(?!.*[.-]{2})[a-zA-Z0-9_%+-][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/v: Invalid character in character class , I don't know where the error is and the check with https://regex101.com/ and according to my expression it has no errors, could you tell me where the error is, or what I missed or what change that I did not realize over time, I am using google chrome as a browser.

The regular expression I use is this: <input type="email" pattern="^(?!.*([.-])\1)(?!.*([.-])$)(?!.*[.-]$)(?!.*[.-]{2})[a-zA-Z0-9_%+-][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$" />

here is just the regular expression:

^(?!.*([.-])\1)(?!.*([.-])$)(?!.*[.-]$)(?!.*[.-]{2})[a-zA-Z0-9_%+-][a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

these are valid emails

these are invalid emails:

  1. email@em
  2. email@example.
  3. [email protected]
  4. [email protected].
  5. [email protected]
  6. [email protected]
  7. [email protected]
  8. [email protected]
  9. [email protected]
  10. [email protected].
  11. [email protected]
  12. [email protected]
  13. [email protected]
  14. test/[email protected]
  15. ,[email protected]
  16. email @test.com.eu
  17. email @test.com.eu
A P I
  • 27
  • 4
  • the error message says you use `/...../v` which does make your expression invalid and produces exactly the error you see - try setting the `v` option in [regex101.com](https://regex101.com/r/4hm6Oh/1) and your errors will appear as if by majicks – Jaromanda X Aug 18 '23 at 05:01
  • Why the regex in the first place? Browsers perform validation [already](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/email#basic_validation) – trincot Aug 18 '23 at 05:48
  • Does this answer your question? [HTML5 Email Validation](https://stackoverflow.com/questions/19605773/html5-email-validation) – InSync Aug 18 '23 at 09:36

1 Answers1

-3

this regex should work

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Ryan M
  • 18,333
  • 31
  • 67
  • 74