Tuesday, May 19, 2009

Important ASP.NET Validation Expressions for RegularExpressionValidator

Some important ValidationExpression for RegularExpressionValidator
[ASP.NET]:

a) Name :: Allowed Characters: A-Z a-z and .
==> ValidationExpression="^[a-zA-Z''-'\.\s]{1,140}$"


b) Comments :: Allowed Character: A-Z a-z 0-9 , . ! and -
==> ValidationExpression="^[0-9a-zA-Z''-'\,\.\!\-\s]{1,140}$"


c) Valid Email Address :: i.e. yourname@companyname.com
==> ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"


d) Valid US Phone Number :: i.e. XXX-XXX-XXXX
==> ValidationExpression="((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}"


e) Valid Date:: i.e. 1900-01-01
==> ValidationExpression="(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])
"


f) Valid Number Only:: i.e. 1900-01-01
==> ValidationExpression="\d+"



source: http://www.regular-expressions.info/dates.html





*** Another Email Address Validator :: i.e. firstname.lastname@companyname.com

        protected bool EmailAddressValidatorCaller(string EmailAddress)
        {
            if (!string.IsNullOrEmpty(Bcc) && !IsValidEmailAddress(Bcc))
                throw new ArgumentException("Bcc email address is not valid", "SendMail");
        }

       
        protected bool IsValidEmailAddress(string EmailAddress)
        {
            string MatchEmailPattern = @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
                                       + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                                          [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
                                       + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                                            [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
                                       + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

            return Regex.IsMatch(EmailAddress, MatchEmailPattern);
        }

No comments: