Anyone can make mistakes when entering an email into an input field. Therefore, it is the developer's responsibility to check if the user has entered a valid email string. There are many libraries available for validating email addresses, which we can use with various JavaScript frameworks, but not with plain JavaScript. However, if we want to use any library, we need to use its CDN.
Here we will validate email addresses in plain JavaScript using regular expressions.
We used the following regular expression pattern in Example 1 to validate the email.
let regex = /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/;
Users can follow the explanation of the above regular expressions below.
^ - It is the beginning of the string.
[a-z0-9] - Any character between a through z and 0 through 9 at the beginning of the string.
@ - The string should contain the "@" character after some alphanumeric characters.
[a-z] - There is at least one character between a and z after the "@" character in the string.
\. – The email should contain a dot, followed by some characters, followed by the “@” character
[a-z]{2,3}$ - It should contain two or three alphabetic characters at the end of the string. ‘$’ represents the end of the string.
In the example below, when the user clicks the button, it calls the validateEmail() function. In the validateEmail() function, we use JavaScript’s Prompt () method to get the user’s email input.
After that, we created the regular expression as explained in the above syntax. We use regular expressions to test the user's email input via the test() method, which returns a boolean value based on whether the email matches the regular expression.
<html> <body> <h3>Using the <i> Regular expression </i> to validate email in JavaScript </h3> <div id = "output"> </div> <button onclick = "validateEmail()"> Validate any email </button> <script> var output = document.getElementById('output'); function validateEmail() { let userEmail = prompt("Enter your email.", "you@gmail.com"); let regex = /^[a-z0-9]+@[a-z]+\.[a-z]{2,3}$/; let result = regex.test(userEmail); if (result) { output.innerHTML = "The " + userEmail + " is a valid email address!"; } else { output.innerHTML = "The " + userEmail + " is not a valid email address!"; } } </script> </body> </html>
We used the following regular expression pattern in Example 2 to validate the email.
let regex = new RegExp(/\S+@\S+\.\S+/);
Users can follow the explanation of the above regular expressions below.
\S - It stands for any alphanumeric word.
\. – represents the dot character.
Basically, the above pattern matches email addresses of type word@word.word.
In the example below, we create the email input using HTML. User can enter any email in the input field. After entering the email in the input, the user needs to click on the "Validate email entered" button, which will call the submitEmail() function.
In the submitEmail() function, we use the above regular expression and test() method to check the email string entered by the user.
In the output, the user can enter various emails and observe the output.
<html> <head> <style> div { font-size: 1rem; color: red; margin: 0.1rem 1rem; } </style> </head> <body> <h2>Using the <i> Regular expression </i> to validate email in JavaScript </h2> <div id = "output"> </div> <input type = "email" id = "emailInput" placeholder = "abc@gmail.com"> <br><br> <button onclick = "submitEmail()"> Validate input email </button> <script> var output = document.getElementById('output'); function submitEmail() { let userEmail = document.getElementById('emailInput').value; let regex = new RegExp(/\S+@\S+\.\S+/); let isValid = regex.test(userEmail); if (isValid) { output.innerHTML = "The " + userEmail + " is a valid email address!"; } else { output.innerHTML = "The " + userEmail + " is not a valid email address!"; } } </script> </body> </html>
We learned to use regular expressions to validate email strings. We have seen two regular expressions and explained them so that beginners can understand how to create regular expressions for emails.
Additionally, developers can create custom regular expressions to validate email addresses based on their needs.
The above is the detailed content of How to validate email address using RegExp in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!