-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

  • email@email.com.eu
  • email@example.example.audio
  • test@code.email.eu
  • example-test@test.email.eu
  • test@code-test.com.eu

these are invalid emails:

  1. email@em
  2. email@example.
  3. test@.code.em.eu
  4. test@ui.de.
  5. example.@test.email.eu
  6. test@-code.example.eu
  7. test@testmail.com.eu.audio
  8. code.@code.com
  9. .test@code.com.audio
  10. .test@code.com.audio.
  11. -test@test.code.audio
  12. -test@test.code.audio-
  13. 87-code@test-.hi.code
  14. test/input@test-.com.ar
  15. ,test@code.com.eu
  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