views:

1251

answers:

3

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?

+3  A: 

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

Ian Jacobs
I was afraid of that. It was worth asking though.
Dan Bailiff
A: 

To be more specific, you need to set the ValidateEmptyMessage property of the CustomValdiator to true, otherwise he won't validate emprty input fields. Example:

<script type="text/javascript">
    function clientValidate(sender, args){
        if (args.Value.length == 0) {
            args.IsValid = false;
        }
    }

</script>
 <asp:CustomValidator runat="server" 
        ID="CustomValidator" 
        ControlToValidate="TextBox1"
        ClientValidationFunction="clientValidate"
        ValidateEmptyText="true"
        Text="Error!">
    </asp:CustomValidator>

But, as you can see this is in no way shorter than your former code, so if it is up to me, there is no point in using custom validator in this very case. As for the original question, unfortunately there is no way to make the default RegExpressValidator validate empty input fields.

Genady Sergeev
+1  A: 

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);

    }
}
Gagarin
that's nice! but how about client side?
Genady Sergeev
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); }
Gagarin