I have an an email type input which has an email validation pattern attribute set up that doesn’t seem to trigger as expected.
<form>
<input type="email" placeholder="enter your email" pattern="[A-Za-z0-9._+-']+@[A-Za-z0-9.-]+.[A-Za-z]{2,}$" required>
<button>submit</button>
</form>
It’s kind of weird cause part of it works. The regex exp is supposed to be matched only after some text and a dot and some text again is added after the @
sign. But is also matched instead with no dot and characters after a someText@sometext
pattern. I’ve tested it on some online tools like regextester and works fine.
3
Answers
try this:
add ^ before the pattern
According to the docs:
The specified pattern is compiled with the
u
flag, which prevents meaningless escapes. Yours has one:'
; simply remove the backslash to make the pattern valid.Also, the last
$
is not necessary as HTML adds it by default, as with^
.Try it:
Interesting question, as there are not many similar reports.
Here is the console log from DevTools.
MDN – Regular Expressions – Character class.
So, escaping is not required for the apostrophe,
'
.