Regex-Basics

Most of the time we need to use regex expression for email id, mobile, zip code validations, here are some standard examples of using some regex expressions commonly used.

Regex for Email-id:-
             Using regex expression for email id validation it’s just you are asking user to enter something that at least make some sense, like, somthing@domail.com. You can’t control here that the email id entered here is his own and not just some random calculations.

NOTE:So as suggested its always good hove some kind of confirmation mail on entered email id, with OPT or kind of link for email id validation along with regex expression.


Here the regex expression which will do some work for you for validating email address,

[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}$

This will match a character group (0 to 9, a to z, A to Z), one or more times, followed by an @ sign. Then we have the same group again, and a final dot followed by the top-level domain. We allow a top-level domain between two and four characters, upper case and lower case.

And if you want to use it with Entity framework then use it as below,

[RegularExpression(@"^([-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4})$", ErrorMessage = "Please enter valid email id.")]
public string Email { get; set; }

Same can be done using JavaScript as,

var pattern =/[-0-9a-zA-Z.+_]+@[-0-9a-zA-Z.+_]+\.[a-zA-Z]{2,4}$/;
       if (!pattern.test(document.getElementById("Email")))
        return false;
       else
        return true;};

Example of valid Email ids are,
     asd@asd.com
     asd.@asd.gov.in


Regex for Zip code (US):-

^\d{5}(?:[-\s]\d{4})?$

Description:-
                 ^          :-  Start of the string
                 \d{5}   :-  Match 5 digits (for condition 1, 2, 3)
                 (?:…)  :-  Grouping
                 [-\s]     : - Match a space (for condition 3) or a hyphen (for condition 2)
                 \d{4}   :-  Match 4 digits (for condition 2, 3)
                 …?      :-  The pattern before it is optional (for condition 1)
                 $          :-  End of the string.

Example:-
           1.  12345
           2.  12345-6789
           3.  12345 1234

In similar way you can use regex for mobile number and other things.
e.g. Regex for sting in fromat,  ###-##-#### orr null or  ""

@"^$|^((\d{3})(-\d{2})(-\d{4}))$

In similar way you can use regex for mobile number and other things.
e.g. Regex for sting in fromat,  ###-##-#### orr null or  ""

@"^$|^((\d{3})(-\d{2})(-\d{4}))$


Regex for Password:-
             Password must meet minimum standards 8 characters in length and contain at least one number and one alpha character and one uppercase letter and one lowercase number.

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{8,60})

Description:-
                  (                      :- start of group
                  (?=.*\d)          :- must contains one digit from 0-9
                  (?=.*[a-z])      :- must contains one lowercase characters
                  (?=.*[A-Z])    :- must contains one uppercase characters
                  (?=.*[@#$%]):- must contains one special symbols in the list "@#$%"
                  .                      :- match anything with previous condition checking
                  {8,60}            :- length at least 8 characters and maximum of 60)


Regex for date in mm/dd/yyyy format:-

[RegularExpression(@"^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$", ErrorMessage = "Enter the Date in mm/dd/yyyy format")]

Comments