Basically we can do different type validation in asp.net which are client side and server side email validation.The client side validation has done using jquery,javascript and asp.net validation controls.Right now i will show how to perform the server side validation using regex patterns.
Pattern for email:
public const string EmailPattern = @"^(([\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})$";The below function will call the valid email function which is "IsEmail", when we submit the email text box datanote 1:Parameter-string that contains an E-Mail address. note 2:True, when Parameter-string is not null and note 3: contains a valid E-Mail address; note 4:otherwise false. public static bool IsEmail(string email) { if (email != null) return Regex.IsMatch(email, MatchEmailPattern); else return false; }
if (!IsEmail(email.Text)) { strError += "Please enter a valid email address"; isValid = false; }
No comments:
Post a Comment