The Wayback Machine - https://web.archive.org/web/20240913224300/https://www.geeksforgeeks.org/how-to-validate-email-address-using-regexp-in-javascript/
Open In App

How to validate email address using RegExp in JavaScript ?

Last Updated : 20 Sep, 2019
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an email id and the task is to validate the email id is valid or not. The validation of email is done with the help of Regular Expressions.

Approach 1:

  • RegExp – It checks for the valid characters in the Email-Id (like, numbers, alphabets, few special characters.)
  • It is allowing every special symbol in the email-id (like, !, #, $, %, ^, &, *) symbols in the Email-Id but not allowing the second @ symbol in ID.

Example: This example implements the above approach.




<!DOCTYPE html> 
<html
  
<head
    <title
        How to validate email address
        using RegExp in JavaScript ?
    </title
</head>
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()"
        click here 
    </button
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p
      
    <script
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var email = 'GeeksForGeeks@gmail.com';
          
        up.innerHTML = "Click on the button to check the "
                    + "validity of Email Id.<br>" + email; 
          
        function isEmail(email) {
              
            // Regular Expression (Not accepts second @ symbol
            // before the @gmail.com and accepts everything else)
            var regexp = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[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,}))$/;
              
            // Converting the email to lowercase
            return regexp.test(String(email).toLowerCase());
        }
          
        function GFG_Fun() { 
            down.innerHTML = isEmail(email);
        
    </script
</body
  
</html>


Output:

  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • RegExp – It checks for the pattern like anything@anything.anything
  • Not like the previous example, RegExp is accepting every character along with special character multiple times.

Example 2: This example implements the above approach.




<!DOCTYPE HTML> 
<html
  
<head
    <title
        How to validate email address
        using RegExp in JavaScript ?
    </title
</head>
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <p id = "GFG_UP" style
            "font-size: 15px; font-weight: bold;"
    </p>
      
    <button onclick = "GFG_Fun()"
        click here 
    </button
      
    <p id = "GFG_DOWN" style
        "font-size: 24px; font-weight: bold; color: green;"
    </p
      
    <script
        var up = document.getElementById('GFG_UP'); 
        var down = document.getElementById('GFG_DOWN'); 
        var email = 'Gee%E^%ksForGeeks@gmail.com';
          
        up.innerHTML = "Click on the button to check the "
                +   "validity of Email Id.<br>" + email; 
          
        function isEmail(email) {
              
            // Regular Expression (Accepts every special
            // character along with @ symbol)
            var regexp = /\S+@\S+\.\S+/;
              
            // Converting the email to lowercase
            return regexp.test(String(email).toLowerCase());
        }
          
        function GFG_Fun() { 
            down.innerHTML = isEmail(email);
        
    </script
</body
  
</html>


Output:

  • Before clicking on the button:
  • After clicking on the button:


Similar Reads

How to Validate Email Address without using Regular Expression in JavaScript ?
Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server. An email address must have the following components to be considered valid:Username:
5 min read
JavaScript Program to Validate An Email Address
We will see how to Validate An Email Address in JavaScript. Validating an email address is a common task in web development. It involves ensuring that an email address entered by a user conforms to a valid format. Validating an email address is a normal task we do in development. Ensuring that a user inputs a valid email address can prevent errors,
3 min read
How to check if an email address exists without sending an email ?
In this article, we will learn to check if an address exists without sending an email. Approach: By using some free APIs we can easily check whether an email is valid or not. One of the easiest way to check whether an email address is valid is to use PHP's filter_var() function. Example 1:The following method can only check if the format is a valid
3 min read
How to Send Email using 'git send-email' via Gmail?
Email communication is a crucial aspect of collaborative software development. Whether you're sharing patches, discussing issues, or coordinating with your team, knowing how to send emails directly from your Git repository can be highly efficient. In this article, we will guide you through the process of configuring and using git send-email with Gm
3 min read
How to validate an email in ReactJS ?
Email validation is an important step in every application in order to authenticate user email. It can be achieved using the validator module in ReactJS. The following example shows how to validate the user entered email and checking whether it is valid or not using the npm module in React Application. Creating React Application And Installing Modu
2 min read
How to validate Email Id in jQuery ?
The task is to validate an email using jQuery. There are a few parameters to get the refined email one of them is to check whether the email contains the @ symbol or not. We can check this out using the following methods in jQuery: Table of Content Using the Regular Expression to validate the entered emailUsing the includes() method with the entere
3 min read
How to check if email address is already in use or not using express-validator in Node.js ?
The registration or Sign Up in any website always requires a username. Most of the time we use 'email' to register on a website. The registration email is always unique and must refer to only one user otherwise conflict between the users can happen. To solve this conflict every website must have the functionality to not accept the email that alread
5 min read
How to Insert Email Address into Form using HTML5 ?
In this article, we will learn how to add and get an email address of a user as data of the form by adding an email input field. As we know that an email_id is a vital component of user data. Email address is used for the verification of the user. It is also used to make contact with the submitters directly. Approach: To complete this task, we have
2 min read
Difference between linking an image, website, and email address in HTML
In this article, we will discuss linking an image, a website, and an email address to a website. The HTML img tag is used to link an image to a website.The HTML anchor tag is used to link a website by adding the path of the website in the hypertext reference (href) attribute.To link an email address, we specify mailto at the beginning of the email
3 min read
How to match multiple parts of a string using RegExp in JavaScript ?
In this article, we will try to understand how we could match multiple parts of a string using Regular Expression (RegExp) in JavaScript with the help of both theoretical explanations as well as coding examples too. Let us first understand some below enlightened theoretical details about Regular Expressions in JavaScript and the actual object (RegE
2 min read
JavaScript RegExp toString() Method
The RegExp toString() Method in JavaScript is used to return the string value of the regular expression. Syntax: RegExpObject.toString() Example 1: This example returns the string value of the regular expression. C/C++ Code function geek() { let regex = new RegExp(&quot;[a-c]&quot;, &quot;gi&quot;); let rex = regex.toString(); console.log(rex); } g
1 min read
JavaScript RegExp (x|y) Expression
The RegExp (x|y) Expression in JavaScript is used to search any of the specified characters (separated by |). Syntax: /(x|y)/ or new RegExp("(x|y)") Syntax with modifiers: /(x|y)/g or new RegExp("(x|y)", "g") Example 1: This example searches the word "GEEKS" or "portal" in the whole string. C/C++ Code function geek() { let str1 = &quot;GEEKSFORGEEK
2 min read
JavaScript RegExp [abc] Expression
The RegExp [abc] Expression in JavaScript is used to search any character between the brackets. The character inside the brackets can be a single character or a span of characters. [A-Z]: It is used to match any character from uppercase A to Z.[a-z]: It is used to match any character from lowercase a to z.[A-z]: It is used to match any character fr
2 min read
JavaScript RegExp [^abc] Expression
The RegExp [^abc] Expression in JavaScript is used to search for any character which is not between the brackets. The character inside the brackets can be a single character or a span of characters. [A-Z]: It is used to match any character from uppercase A to uppercase Z.[a-z]: It is used to match any character from lowercase a to lowercase z.[A-z]
2 min read
JavaScript RegExp i Modifier
The RegExp i Modifier in JavaScript is used to perform case-insensitive matching in the string. Syntax: /regexp/i or new RegExp("regexp", "i") Example 1: This example matches the word "geeks" or "Geeks" (case insensitive) and displays it. C/C++ Code function geek() { let str1 = &quot;Geeksforgeeks is the computer &quot; + &quot;science portal for g
2 min read
JavaScript RegExp g Modifier
The RegExp g Modifier in JavaScript is used to find all the occurrences of the pattern instead of stopping after the first match i.e it performs a global match. Syntax: /regexp/g or new RegExp("regexp", "g") Example 1: This example searches the word "geeks" in the whole string. C/C++ Code function geek() { let str1 = &quot;geeksforgeeks is the comp
2 min read
JavaScript RegExp m Modifier
The RegExp m Modifier in JavaScript is used to perform multiline matching. It takes the beginning and end characters (^ and $) as working when taking over multiple lines. It matches the beginning or end of each line. It is case-sensitive. Syntax: /regexp/m or new RegExp("regexp", "m") Example 1: This example searches the word "geeksforgeeks" at the
2 min read
JavaScript RegExp [0-9] Expression
The RegExp [0-9] Expression in JavaScript is used to search any digit which is between the brackets. The character inside the brackets can be a single digit or a span of digits. Syntax: /[0-9]/ or new RegExp("[0-9]") Syntax with modifiers: /[0-9]/g or new RegExp("[0-9]", "g") Example 1: This example searches the digits between [0-4] in the whole st
2 min read
JavaScript RegExp [^0-9] Expression
The RegExp [^0-9] Expression in JavaScript is used to search any digit which is not between the brackets. The character inside the brackets can be a single digit or a span of digits. Syntax: /[^0-9]/ or new RegExp("[^0-9]") Syntax with modifiers: /[^0-9]/g or new RegExp("[^0-9]", "g") Example 1: This example searches the digits which are not presen
2 min read
JavaScript RegExp \S Metacharacter
The RegExp \S Metacharacter in JavaScript is used to find the non-whitespace characters. The whitespace character can be a space/tab/new line/vertical character. It is the same as [^\t\n\r]. Syntax: /\S/ or new RegExp("\\S")Syntax with modifiers: /\S/g or new RegExp("\\S", "g")Example 1: This example matches the non-whitespace characters. C/C++ Cod
1 min read
JavaScript RegExp . Metacharacter
The RegExp . Metacharacter in JavaScript is used to search single characters, except line terminator or newline. Syntax: /regexp./ or new RegExp("regexp.") Syntax with modifiers: /regexp./g or new RegExp("regexp.", "g") Example 1: This example searches the words whose starting character is "A" and ending character is "C" with only one character in
2 min read
JavaScript RegExp \W Metacharacter
The RegExp \W Metacharacter in JavaScript is used to find the nonword character i.e. characters which are not from a to z, A to Z, 0 to 9. It is the same as [^a-zA-Z0-9]. Syntax: /\W/ or new RegExp("\\W") Syntax with modifiers: /\W/g or new RegExp("\\W", "g") Example 1: This example searches the non words characters. C/C++ Code function geek() { le
2 min read
JavaScript RegExp \w Metacharacter
The RegExp \w Metacharacter in JavaScript is used to find the word character i.e. characters from a to z, A to Z, 0 to 9. It is the same as [a-zA-Z_0-9]. Syntax: /\w/ or new RegExp("\\w") Syntax with modifiers: /\w/g or new RegExp("\\w", "g") Example 1: This example searches the lowercase characters, uppercase characters, and underscore. C/C++ Code
2 min read
JavaScript RegExp lastIndex Property
The lastIndex Property in JavaScript is used to specify the index at which to start the next match. If "g" modifier is not present this property will not work. It can be used to return the position of character immediately after the previous match. Syntax: RegexObj.lastIndex Example 1: This example checks if the string contains "Geek" in it. C/C++
2 min read
JavaScript RegExp \b Metacharacter
The RegExp \b Metacharacter in JavaScript is used to find a match at the beginning or end of a word. If a match is found it returns the word else it returns NULL. Syntax: /\b/ or new RegExp("\\b") Syntax with modifiers: /\b/g or new RegExp("\\b", "g") Example 1: This example matches the word "GeeksforGeeks" at the beginning of the string. C/C++ Cod
2 min read
JavaScript RegExp \B Metacharacter
The RegExp \B Metacharacter in JavaScript is used to find a match that is not present at the beginning or end of a word. If a match is found it returns the word else it returns NULL. Syntax: /\B/ or new RegExp("\\B") Syntax with modifiers: /\B/g or new RegExp("\\B", "g") Example 1: This example matches the word "for" which is not present at the beg
2 min read
JavaScript RegExp multiline Property
The multiline Property property in Javascript is used to specify whether the "m" modifier is set or not. If the "m" modifier is set than this property returns true else false. Syntax: RegexObj.multiline Return Value: It returns true if m modifier is set, else false. Example 1: This example checks if the regular expression contains m Modifier or not
1 min read
JavaScript RegExp \n Metacharacter
The RegExp \n Metacharacter in JavaScript is used to find the newline character. If it is found it returns the position of the character else it returns -1. Syntax: /\n/ or new RegExp("\\n") Example 1: This example searches for the newline character in the string. C/C++ Code function geek() { let str1 = &quot;GeeksforGeeks@_123_$&quot;; let regex4
2 min read
JavaScript RegExp \f Metacharacter
The RegExp \f Metacharacter in JavaScript is used to find the form feed character (form feed is a page-breaking ASCII control character). If it is found it returns the position else it returns -1. Syntax: /\f/ or new RegExp("\\f") Example 1: This example searches for the form feed character in the string. C/C++ Code function geek() { let str1 =
2 min read
JavaScript RegExp \r Metacharacter
The RegExp \r Metacharacter in JavaScript is used to find the carriage return character (Carriage return means to return to the beginning of the current line without advancing downward). If it is found it returns the position else it returns -1. Syntax: /\r/ or new RegExp("\\r") Example 1: This example searches for the carriage return character in
2 min read
Article Tags :