How to Validate an Email Address in JavaScript Example

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

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *