I have an email validation regex, it goes like this ^(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}$
I checked it in a couple of sandboxes and it works as intended. However when I run my code (a simple html form + a bit of css) in a browser (via Live Server or GH pages) it behaves differently. I can put there something like g^&@h and it displays as a valid email. What could be the issue? The corresponding bit of code:
<div class="form__field">
<input class="form__input" type="email" name="email" pattern="^(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}$" placeholder="E-Mail" required />
<span class="form__error">Example: [email protected]</span>
</div>
full code: https://github.com/glpsch/RegExp-exercise
I tried different browsers, the behaviour is the same.
0
A literal -
must be escaped in a character class in an HTML/JavaScript regex according to the MDN documentation:
In addition to
]
and, the following characters must be escaped in
character classes if they represent literal characters:(
,)
,[
,{
,}
,
/
,-
,|
. This list is somewhat similar to the list of syntax
characters, except that^
,$
,*
,+
, and?
are not reserved inside
character classes, while/
and-
are not reserved outside character
classes (although/
may delimit a regex literal and therefore still
needs to be escaped).
You also don’t need to anchor your regex with ^
and $
since the browser does it for you, so the following regex would work:
(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}
Demo:
@keyframes errors {
from {
margin-top: -5px;
opacity: .1;
}
to {
margin-top: 10px;
opacity: 1;
}
}
html {
font-family: Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
}
.container {
max-width: 600px;
margin: auto;
}
.header {
max-width: 600px;
height: 80px;
margin: auto;
display: flex;
align-items: center;
}
.header__logo {
width: 150px;
}
.header__logo-sub {
margin-left: 10px;
margin-bottom: 25px;
font-size: 0.6em;
}
.form {
max-width: 600px;
margin: auto;
}
.block {
margin-top: 60px;
border-radius: 3px;
}
.cover {
display: flex;
align-items: center;
margin-top: 30px;
margin-bottom: 20px;
}
.cover__image {
width: 110px;
min-width: 110px;
height: 110px;
background-image: url(regex.png);
background-size: cover;
background-position: center;
border-radius: 50%;
}
.cover__heading {
margin-left: 30px;
font-size: 2.5em;
line-height: 1;
}
.form__field {
margin-bottom: 36px;
}
.form__fieldset {
border: none;
margin: 0;
padding: 0;
}
.form__error {
display: none;
position: absolute;
margin-top: 10px;
color: #df4b41;
text-align: left;
font-size: 14px;
animation: errors .1s ease-in-out;
}
.form__input {
display: block;
width: 100%;
border: none;
outline: none;
border-radius: 2px;
border-bottom: 2px solid #ccc;
padding: 10px 0;
box-sizing: border-box;
font-size: 16px;
}
.form__button {
width: 200px;
height: 48px;
font-weight: bold;
font-size: 14px;
border-radius: 3px;
border: none;
text-align: center;
cursor: pointer;
background-color: #ffdb4d;
color: #000;
}
.form__input:valid:not(:placeholder-shown) {
border-color: #ffdb4d;
}
.form__input:invalid:not(:placeholder-shown) {
border-color: #df4b41;
}
.form__input:invalid:not(:placeholder-shown) + .form__error {
display: block;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Regular Expressions</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<header class="header">
<p class="header__logo-sub">Regular expressions</p>
</header>
<main class="container">
<div class="cover">
<div class="cover__image"></div>
<h1 class="cover__heading">RegExp</h1>
</div>
<div class="block">
<form class="form" name="register">
<h3>Registration form</h3>
<fieldset class="form__fieldset">
<div class="form__field">
<input class="form__input" type="text" name="name" pattern="(^[А-Я][а-яё]*(-[А-Я][а-яё]*)?$)|(^[A-Z][a-z]*(-[A-Z][a-z]*)?$)" placeholder="Name / Имя" minlength="2" maxlength="20" required />
</div>
<div class="form__field">
<input class="form__input" type="email" name="email" pattern="(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}" placeholder="E-Mail" required />
<span class="form__error">Example: [email protected]</span>
</div>
<div class="form__field">
<input class="form__input" type="tel" name="tel" placeholder="Telephone number" pattern="^+?d{1,3}s?(?d{3})?s?-?d{3}s?-?(?:d{2}s?-?)d{2}$" minlength="11" maxlength="20" required/>
<span class="form__error">Example: +7 (900) 000-00-00 or +79000000000</span>
</div>
<div class="form__field">
<input class="form__input" type="url" name="url" pattern="^(?:(https://)|(http://))(?:((d{1,3}.){3}d{1,3})|((?:www.)?(?:w+.?)+w+))(?::[1-9]d{1,4})?(?:[a-zA-Z0-9]+/?)*#?$" placeholder="Your website" required/>
<span class="form__error">Example: http://example.com</span>
</div>
<button class="form__button" type="submit">Submit</button>
</fieldset>
</form>
</div>
</main>
</body>
</html>