How to validate email address using RegExp in JavaScript?
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.
Regular expression used in Example 1
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.
Example 1
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>
Regular expression used in Example 2
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.
Example 2
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

Enhance Your Code Presentation: 10 Syntax Highlighters for Developers Sharing code snippets on your website or blog is a common practice for developers. Choosing the right syntax highlighter can significantly improve readability and visual appeal. T

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

This article presents a curated selection of over 10 tutorials on JavaScript and jQuery Model-View-Controller (MVC) frameworks, perfect for boosting your web development skills in the new year. These tutorials cover a range of topics, from foundatio

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was
