Monday, 2 March 2015

Regular Validation Expressions In ASP.Net To Validate Input From User

 For Email:

  validationexpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

 validationexpression="^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$" 

For  money(accepting decimal and numerics):

 validationexpression="^[0-9]\d*(\.\d+)?$"

For numerics only:

validationexpression="^[0-9]+$"

For Zip:

-->(accept 1 to 10 digits):


validationexpression="\d{1,10}"

-->(accept three conditions for zip-code.

    12345 
    12345-6789 
    12345 1234 

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

explanation:

  • ^ = 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.

For Date:(MM/YY):

validationexpression="^(0?[1-9]|1[0-2])/\d{2}$"

For Phone Number:

--->Validates a U.S. phone number. It must consist of 3 numeric characters, optionally enclosed in parentheses, followed by a set of 3 numeric characters and then a set of 4 numeric characters.


(425) 555-0123

425-555-0123

425 555 0123

1-425-555-0123

validationexpression="^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$"


-->Ex:Indian Phone Number validation
+919000000098
or 
09000000098
or 
9000000098
or 
+91-9000000098
or 
9908716458
validationexpression="^((\\+91-?)|0)?[0-9]{10}$"

For Password:

-->Validates a strong password. It must be between 8 and 10 characters, contain at least one digit and one alphabetic character, and must not contain special characters.

validationexpression="(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$"

For URL:

validationexpression="^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$"

No comments:

Post a Comment