This is my JavaScript code to check email validation in Javascript. But it is not working. When I entered the wrong Email, my email validation code was successful.
function validateEmailId() { var email = document.getElementById("email_id").value; var reg = "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}/"; var res = reg.test(email); if (res == false) { alert("Failed"); }else{ alert("success") } return true; } <form action="#" onsubmit="return validateEmailId()"> <div class="inputgrp"> <input type="text" placeholder="Email Address" id="email_id"> <button type="submit">Submit</button> </div> </form>
javascript email validation
I think you created a string instead of creating RegExp.
Please change
var reg = "^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}/";
to
var reg = /^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
It will work. cheers..
Test your knowledge with interactive quizzes.
Prepare for interviews with curated question sets.
Ask your coding-related doubts and get answers.
Earn certifications to enhance your resume.
Hands-on projects to improve your skills.
Test your knowledge with interactive quizzes.
Prepare for interviews with curated question sets.
Add your technical blogs and read technical topics.
Earn certifications to enhance your resume.
Hands-on projects to improve your skills.