Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Regular expressions for Email Validation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 488
    Comment on it

    Regular expressions for Email Validation

    In this post, we are going to elaborate, how can we implement Regular Expressions for email validation in JavaScript. There are many validation example and function are available, but we are going to elaborate, how can we create those regular expressions for email validation and the brief view of regular expressions elements. Before starting it, first we have to know something about Regular Expressions. Below overview of Regular expression and describe example of email validation, helps you to understand Regular Expressions

    Overview of Regular Expressions

    In theoretical computer science and formal language theory, a regular expression (abbreviated regex or regexp and sometimes called a rational expression)[1][2] is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "Find and replace"-like operations.

    - By Wikipedia

    A regular expression, in short regex, or regexp. A regular expression is a unique text string for representing a search pattern. The regular expression is used to execute impression-matching and search and replace action on text.

    In JavaScript, regular expressions is also an object that represents an impression of the characters. These impressions or patterns are implemented with the 'exec' and 'test' method of RegExp, and also through matching, replacing and splitting methods of string.

    - Syntax

    /pattern or impression/ modifiers;

    - Example

    var regPattern = /w3schools/i
    Detail Description of Example: => /w3schools/i is a regular expression.
    => w3schools is a pattern or impression (to be used in a search).
    => i is a modifier (modifies the search to be case-insensitive).

    -By W3C Schools

    Above, I define what is Regular Expressions. There are so many other elements which consist with regex or regexp. Here are the list and its properties which help to implement a pattern to search and execute the function.

    Modifiers

    Modifiers are the elements which help us to execute case-insensitive and global searches. Here are the element lists of Modifiers.

    Modifier

    Description

    i

    To Execute case-insensitive matching

    g

    To Execute a global Match function

    m

    To execute multiple line matching function

    Brackets or Expressions

    Brackets helps to get a range of characters. Here are the Brackets or Expressions list with description.

    Brackets or Expressions

    Description

    [abc]

    To search any alphabetic character between the expressions or brackets

    [^abc]

    To search any alphabetic character Not between the expressions or brackets

    [0-9]

    To search any digit or numeric value between the expressions or brackets

    [^0-9]

    To search any digit or numeric value Not between the expressions or brackets

    [x|y]

    To search any of the options stated or describe

    These are the relevant elements which helps to create a proper RegEx patterb for email validation. Below I am going to elaborate the 'regex' with a proper example. Just go through the below HTML and JavaScript code to get a proper result of email validation with alert.

    Brief Example of Email Validation

    <html>
    <head>
    <meta  http-equiv="Content-Type" content="text/html;  charset=ISO-8859-1">
    <title>Validate  Email Address</title>
    <script  language="javascript">
        function  checkEmail(inputvalue){ 
            var  pattern=/^([a-z0-9_.-])+@([a-zA-Z_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
             if(pattern.test(inputvalue)){ 
                alert("Email  is correct");
            }
            else{
                alert("Enter  Correct Email Address");
            }
         }
    </script>
    </head>
    <body>
        <form  name="signupform">
        Enter  your email:
    
            <input  name="email" type="text" class="inputs" id="email_address"  
            placeholder="Your Email Address"  />
    
            <input  name="summit" type="submit" value="Check"  onClick="checkEmail
            (document.signupform.email.value)" /> 
        </form>
    </body>
    </html>
    

    In above example, We had create a function (checkEmail), where we create a pattern of some expressions. Below I mentioned the same pattern with description.

    Above example Expression pattern is :
    /^([a-z0-9_.-])+@([a-zA-Z_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

    Below characters description will helps you to understand that how these characters pattern work and how we can validate the email values.

    / => To Start a Regex Pattern

    ^ => To start the string match at the start time of pattern.

    () => To make every string inside the note an explanatory group of characters

    [] => To match only one character inside the brackets.

    a-z => To match only lowercase letters in pattern.

    A-Z => To match only Uppercase letters in pattern

    0-9 => To match only only numeric digits in a pattern

    _.- => To match any literal symbol in pattern.

    + => To match previous expression pattern at least once.

    @ => To match an actual @ symbol in pattern.

    \. => To match an actual symbol of . in pattern.

    / => To stop an expressions pattern.

    There are more other type of characters which helps us to create specific pattern or create more standard pattern for email validation. These are describe below.

    \w => To match any word (letters, digits, underscores)

    \+\. => To match an actual symbol of + or . in expression pattern.

    * => To match the previous one expression in zero or infinite times.

    \w- => To match any word or a symbol of -

    [\w-]*\. => To match a word or a dash at least zero times, followed by a literal .

    [a-z]{2,3} => To match lowercase letters at least 2 times but not more than 3 times.

    \d+ => To match at least 1 digit in pattern.

    $ => To match the end of the string.

    These are all which can help us to create an expression pattern to validate the email as per the email standards. Now this is all up-to you that which type of pattern approach you will follow.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: