Email Regular Expression Validation
Can anyone correct the expression below to also not allow blank field?
<asp:RegularExpressionValidator ID="expEmail" runat="server" ControlToValidate="txtEmail" ErrorMessage="valid email address required" ValidationExpression="^([a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-开发者_如何学编程zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]){1,70}$"></asp:RegularExpressionValidator>
One of the solutions (not a best one) is to implement some regular expression:
^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
Sample: someone@example.com
But actually, it's very hard to write and maintain a good email validation regexp. You shouldn't trust 100% your regex. You can find several articles about that difficulty on the Internet:
- It's Impossible to Validate an Email Address (elliot.land) - discussion on Hacker News
- Stop Validating Email Addresses With Regex
- Email Address Regular Expression That 99.99% Works.
Add a required field validator as well - I think the regex validator will only fire if there is text in the field to look at.
Can't you make something like
if (txtEmail.Text.Trim().length > 0) then
validate
I think that since regular expressions are rather complex, anything that can be done outside the regular expression should be done outside the regular expression, this should make the code more readable, but that is just me.
Your regular expression is quite complex, here you should find a simpler one. Anyways, what you can do is something like this: ^(regex){1}$
You need to use an additional validator - asp:RequiredFieldValidator
<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="Message!" />
Any of ASP.NET 2.0 validators based on BaseValidator
(except RequiredFieldValidator itself of course) doesn't checks specified field to be empty.
Btw, to make custom UserControl
being able to be checked with such validators, use
[System.Web.UI.ValidationProperty("PropertyName")]
If you want to treat this field as required one then it is better go for RequiredFieldValidator.
<asp:RequiredFieldValidator ID="reqEmail" ControlToValidate="txtEmail" runat="server" ErrorMessage="Email address required"></asp:RequiredFieldValidator>
Thanks,
Another choice is to use .net CustomValidator, set its ValidateEmptyText to True, and for email address validation you can use a JavaScript function which will be specified in ClientValidationFunction property of validator.
void Page_Load(Object sender, EventArgs e)
{
string uNameExpr = "^[a-zA-Z](.{1,9})$";
string phoneExpr =
"((\\(\\d{3}\\) ?)|(\\d{3}-))?\\d{3}-\\d{4}";
// Define validation expressions.
RegExprVal1.ValidationExpression = uNameExpr;
RegExprVal2.ValidationExpression = phoneExpr;
ReqFldVal1.Text = "User name is required";
RegExprVal1.Text = "Must be between 2 to 10 characters";
RegExprVal2.Text = "Please provide a valid number: (425) 555-0187";
// ErrorMessages appear in ValidationSummary.
RegExprVal1.ErrorMessage = "Incorrect UserName format. Name" +
" can be 2 to 10 characters long";
ReqFldVal1.ErrorMessage = "User name required";
RegExprVal2.ErrorMessage =
"Please provide a valid number: (000) 000-0000";
}
void OnCmdClick(Object sender, EventArgs e)
{
if (Page.IsValid)
{
ActiveForm = Form2;
}
}
This can be done with simply using Regex
class in System.Text.RegularExpressions
:
private bool ValidateEmail()
{
string email = txtemail.Text;
Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
Match match = regex.Match(email);
if (match.Success)
return true;
else
return false;
}
精彩评论