I'm trying to use the HTML email input type along with the pattern attribute for the first time. However, I'm running into an invalid regex error in Firefox, even though my regex works perfectly fine when tested on regex101.com. Initially, I'm using a simple regex to get it working, and then I'll move on to my custom regex. Here's my HTML code I'm working with:
``
When checking in Firefox v142.0, I get the error:
`Unable to check because '/[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,}$/' is not a valid regexp: invalid character in class in regular expression`
Can anyone offer some guidance on how to resolve this?
4 Answers
Yeah, and even if it seems solid, regex emails can often fail. Checking via email verification is much more reliable and it avoids the hassle of complicated regex patterns.
You're correct to consider regex, but don’t forget that it won’t cover all possibilities—email formats can be tricky! Why not implement an email confirmation instead? It’s straightforward and ensures the address is reachable.
It looks like the problem is with the last `-` in your character class `[a-z0-9.-]`. Make sure to escape it properly. You should change that part to avoid confusion. Try this corrected version: `[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,}$`.
Just a heads up, regex isn’t the best way to validate emails. There are so many edge cases that can lead to false negatives. It’s safer to validate by sending an actual email verification link!

Related Questions
How to Build a Custom GPT Journalist That Posts Directly to WordPress
Cloudflare Origin SSL Certificate Setup Guide
How To Effectively Monetize A Site With Ads