0
votes

I wish to validate an e-mail input string, so it's a valid email. And I wish to check the length of the e-mail string, so it isn't more than 60 characters. I'm using RegularExpressionValidator.. I have two of them, one to validate e-mail pattern, and one to validate length. But that doesn't seem to work. I can use one at a time with no problems, but if I use both of them, they both display error where there is no error.

Can these be combined? Or is there a better regualexpression that validates e-mail and length at the same time?

Controls:

<%-- Validate EmailAddress --%>
<asp:RequiredFieldValidator ID="EmailAddressRequiredFieldValidator" runat="server" 
ErrorMessage="..." ControlToValidate="EmailAddressInsertTextBox" Display="None" ValidationGroup="InsertValidationGroup" />          

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="EmailAddressInsertTextBox" ErrorMessage="......" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="None" ValidationGroup="InsertValidationGroup" />

<asp:RegularExpressionValidator ID="EmailAddressRegularExpressionValidator2" runat="server" ControlToValidate="EmailAddressInsertTextBox" ErrorMessage="..............." ValidationExpression="^[\w\s]{1,60}$" Display="None" ValidationGroup="InsertValidationGroup" />
3
What are the patterns that you are currently using? I'm sure they could be combined into a single regex. - Servy
1: "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" 2: "^[\w\s]{1,60}$" - user1121487

3 Answers

1
votes

I’m not saying that this is the best approach here, but if you want to combine two regexes, you can usually go for positive lookbehind.

The code below is matched by a field value consisting of 1–60 characters from start to end. The lookbehind ensures that the value matches your email pattern, optionally preceded and succeeded by whitespace:

ValidationExpression="^.{1,60}$(?<=\s*\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*\s*)"
0
votes

There's no real reason this shouldn't work. Best practice is to apply them separately, so that you can have two separate error messages for the different problems.

There's an example of two working together on the same control, here: http://www.informit.com/articles/article.aspx?p=101137&seqNum=3 (see "Listing 3.6")

Perhaps if you provide your code, it might turn out to be something else.

Edit: I tried the code you provided. I changed the length validator regex to ^.{1,60}$ and it works for me. I also changed display to dynamic, so I could test it, but that won't change the result of Page.IsValid.

0
votes

I believe the Regex Validator will only fire if there is text in the field to look at. You need additional validator, this is the asp:RequiredFieldValidator

<asp:RequiredFieldValidator runat="server" ControlToValidate="txtEmail" ErrorMessage="You must input an email address" />

Let's make this even more simple by creating javascript function and calling it in our validator

function validateEmail(email) {
    var regex = /^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;
    // 60 char check
    return email.length <= 60 && regex.test(email);
}