I have for a frontendmentorchallenge tried to do email validation for webpage. I have made a validation function to display error message when there is wrong email id entered. But it isn't working. I have given the html and JavaScript
function validate() {
var email = document.getElementById("email").value;
var reg = "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$";
var result = reg.test(email);
if (result == false) {
alert("sorry");
return false;
}
return true;
}
<form action="#" method="post" onsubmit="return validate()">
<div class="inputgrp">
<input type="text" placeholder="Email Address" id="email">
<img src="/images/icon-error.svg" alt="" class="error">
<button type="submit"><img src="/images/icon-arrow.svg " ></button>
<div class="errormsg">
<small>please provide a valid Email</small>
</div>
</div>
</form>
/in the reg and remove the 3 so I also can email you:var reg = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,}$/;var reg= "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$";does not create a regex, but a string. To create a regex use this:var reg= /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;<input type="email" required />, and have the validation done for you?