RegEx: different results in sandbox and browser [duplicate]

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
</code>
<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> </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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}
</code>
<code>(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,} </code>
(?:[w-]+.?)+@(?:[w-]+.?)+[w-].[a-z]{2,}

Demo:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@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;
}</code>
<code>@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; }</code>
@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;
}
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><!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></code>
<code><!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></code>
<!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>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật