asp.netregexvalidationfluentvalidationfluentvalidation-2.0

Regular Expression Validator does not validate empty textbox


I would like to validate the textbox for specific text and it must not be blank. But the regular expression validator is not validating if the text box is BLANK. However, it validates if I type something in the text box.

How can I make regular expression to trigger even if the text box is empty?

Should I use Required Validator + Regex Validator at the same time? Thanks.

<asp:TextBox ID="txtcard" runat="server" MaxLength="16"></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" 
ControlToValidate="txtcard" ErrorMessage="Please type credit card no" 
ValidationExpression="^\d{16}$"></asp:RegularExpressionValidator>

Solution

  • I would generally do as you suggest and have a required validator as well. This would allow you to have a different message for each rule.

    Another option that I would recommend any web developer look at is the JQuery validation plugin. If you combine this with Fluent Validation, you can keep all of your validation rules for your business objects in one place and you can validate on the Client side and at the Server using those same rules.

    JQuery Validation

    Fluent Validation