Navigate back to the homepage

How to check if a string is a valid email in javascript?

Infinitbility
JavaScript
May 26th, 2022 · 1 min read

Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, I’m going to show you, how do you check if a string is a valid email in javascript, here I will use regex expression to valid email string.

Which regex we are going to use, it’s a pass bunch of test cases which you can see here.

Let’s start the today’s tutorial title How to check if a string is a valid email in javascript?

Here, we will create a custom function that expects an email string and validate the string using regex, and it will return true and false based valid email string or not.

So, we are going to do

  1. create a validated email function
  2. use functions with the different email addresses
  3. console which email is valid and which is not.
1// create validate email function
2const validateEmail = (email) => {
3 return String(email).toLowerCase().match(/^(([^<>()[\]\\.,;:\[email protected]"]+(\.[^<>()[\]\\.,;:\[email protected]"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
4};
5
6
7// use function with diffrent email address
8// console which email is valid and which not.
9if(validateEmail("[email protected]")){
10 console.log("[email protected] is a valid email");
11} else {
12 console.log("[email protected] is a invalid email");
13}
14
15if(validateEmail("[email protected]")){
16 console.log("[email protected] is a valid email");
17} else {
18 console.log("[email protected] is a invalid email");
19}

When running the above program it should which email address is valid and which is not. let’s check the output.

javascript, check if a string is a valid email example
javascript, check if a string is a valid email example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to check if a string is empty or undefined in javascript?

Example of check if a string is empty or undefined in javascript with step by step guide.

May 26th, 2022 · 1 min read

How to check if a string is a substring of another string in javascript?

Example of check if a string is a substring of another string in javascript with step by step guide.

May 25th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]