How to Validate an Email Address in JavaScript Example


If you are developing a web application and creating login, registration and contact forms like that in this app. And at that time you have to validate email in these forms. So that the user submits the correct information and gets the correct email in the database. So for this you can do this by using many methods in JavaScript.

Email validation in javascript; In this tutorial, you will learn How to validate an email address in javascript with example.

How to Validate an Email Address in JavaScript

By using these most popular methods, you can easily validate email address in javaScript:

  • Email Validation using Regular Expression
  • JavaScript’s built-in methods for Email Validation

Email Validation using Regular Expression

You can easily validate email address using javaScript regular expression or regex.

Here’s an example of how to validate an email address using regex in JavaScript:

function validateEmail(email) {
  const regex = /^[\w.-]+@[a-zA-Z_-]+?\.[a-zA-Z]{2,}$/;
  return regex.test(email);
}

const email = "[email protected]";
console.log(validateEmail(email));  // Output: true

Here’s a breakdown of the code above:

  • ^ and $ ensure that the entire string matches the pattern from start to end.
  • [\w.-]+ matches one or more word characters, dots, or hyphens at the beginning of the email address.
  • @ matches the “@” symbol.
  • [a-zA-Z_-]+? matches one or more letters, underscores, or hyphens in the domain name.
  • \. matches a dot character.
  • [a-zA-Z]{2,} matches two or more letters for the top-level domain (e.g., com, org, co.uk, etc).

JavaScript’s built-in methods for Email Validation

In the second method, you can validate email addresses using built-in methods and without using regular expressions.

Here’s an example of how to validate an email address using indexOf() and lastIndexOf() in JavaScript:

function validateEmail(email) {
  const atIndex = email.indexOf("@");
  const dotIndex = email.lastIndexOf(".");

  return atIndex > 0 && dotIndex > atIndex && dotIndex < email.length - 1;
}

const email = "[email protected]";
console.log(validateEmail(email));  // Output: true

Conclusion

Validating email addresses is a fundamental task when dealing with user input. By employing regular expressions or JavaScript’s built-in methods, you can ensure the accuracy of email addresses in your web applications.

Recommended Tutorials

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?