How to combine RegularExpressionValidator control and RequiredFieldValidator?

13,882

Solution 1

You can roll your own CustomValidator, combining the functionality. Other than that, no not to my knowledge.

Solution 2

One common problem is that the validator component still takes space when it is not shown, which looks odd if you have several and i.e. the last one is triggered leaving a larger gap to the asterisk or other error marker. This can be easily solved by adding:

display="Dynamic"

...to the validator.

But it does not solve the problem of several trigging at the same time which will still show many validator errors in a row. A custom validator would then probably be the best solution.

Solution 3

You can override EvaluateIsValid method

    public class RegularExpressionValidatorEx : RegularExpressionValidator
{
    protected override bool EvaluateIsValid()
    {
        string controlValidationValue = base.GetControlValidationValue(base.ControlToValidate);
        if ((controlValidationValue == null) || (controlValidationValue.Trim().Length == 0))
        {
            return false;
        }
        return base.EvaluateIsValid();
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        Page.ClientScript.RegisterStartupScript(GetType(), "customVal", ClientID + @".evaluationfunction = function(val){
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
    return false;
return RegularExpressionValidatorEvaluateIsValid(val);}", true);

    }
}
Share:
13,882
Dan Bailiff
Author by

Dan Bailiff

C# Web Developer

Updated on June 11, 2022

Comments

  • Dan Bailiff
    Dan Bailiff almost 2 years

    I often use regex expression validators that are also a required field. Which leads to what seems like redundant controls on the page. There is no "Required" property of the regex validator which means I need another control. Like this:

    <asp:TextBox ID="tbCreditCardNumber" runat="server" Width="200"></asp:TextBox>
    <asp:RegularExpressionValidator ID="revCreditCardNumber" runat="server"
            ControlToValidate="tbCreditCardNumber" ValidationGroup="CheckoutGroup" ErrorMessage="Invalid Credit Card Number!"
            ValidationExpression="^(3[47][0-9]{13}|5[1-5][0-9]{14}|4[0-9]{12}(?:[0-9]{3})?)$">*</asp:RegularExpressionValidator>
    <asp:RequiredFieldValidator ID="rfvCreditCardNumber" runat='server' ControlToValidate="tbCreditCardNumber" ValidationGroup="CheckoutGroup"
            ErrorMessage="Credit Card Number Required">*</asp:RequiredFieldValidator>
    

    Is there a way to combine the two controls so I don't have to type so much code?

    • Avitus
      Avitus almost 15 years
      Any reason why you wouldn't use a custom validator?
    • dannmate
      dannmate about 9 years
      Why not just check for blanks in your regex? No need for a required field validator that way.
  • Genady Sergeev
    Genady Sergeev almost 15 years
    that's nice! but how about client side?
  • Gagarin
    Gagarin almost 15 years
    I guess, CustomValidator is better idea, however you could do the same trick;), override evaluation method on client side protected override void OnPreRender(EventArgs e) { base.OnPreRender(e); Page.ClientScript.RegisterStartupScript(GetType(), "customVal", ClientID + @".evaluationfunction = function(val) { var value = ValidatorGetValue(val.controltovalidate); if (ValidatorTrim(value).length == 0) return false; return RegularExpressionValidatorEvaluateIsValid(val); }", true); }
  • Dan Bailiff
    Dan Bailiff over 14 years
    I was afraid of that. It was worth asking though.