tags:

views:

408

answers:

6

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>
+4  A: 

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.

Paddy
The required field validator is already used in the question. The question asked by the user is to validate the regular expression written in the required field validator.
Phani Kumar PV
@Phani Kumar PV - Um, no it's not. But you made me look :)
Paddy
@Paddy Not going to work as each error leave the space on the page for the label so the second error is floated over the right etc. Any solution to this?
Pino
@Pino - Set the display property on the validator to dynamic.
Paddy
A: 

RegExp to check email:

^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$

Example [email protected]

Anry
See more at http://www.regular-expressions.info/email.html
abatishchev
A: 

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,

A: 

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")]

abatishchev
A: 

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}$

npinti
A: 

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.

GS_Guy