Explore Topics

Email Validation In Javascript Not Working

Last Updated : 18 Apr, 2025 - Asked By Ashok

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 

Answers
2024-10-15 10:09:14

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..

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.