How to combine RegularExpressionValidator and RequiredFieldValidator control?

I often use regular expression expression validators, which are also a required field. This leads to what looks like redundant controls on the page. There is no Required property to validate the regular expression, which means that 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 need to enter so much code?

+4
source share
4 answers

You can collapse your own CustomValidator by combining functionality. Other than that, I don’t know.

+3
source

One common problem is that the validator component still takes up space when it is not displayed, which looks odd if you have several, and therefore the last one starts up, leaving a larger gap for an 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 triggers at the same time, which will still show a lot of validation errors in the string. Then, probably, the best option would be the elector.

+6
source

You can override the 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); } } 
+1
source

To be more specific, you need to set the ValidateEmptyMessage property for CustomValdiator to true, otherwise it will not check 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 by no means shorter than your previous code, so if it depends on me, in this case there is no point in the user validator. Regarding the original question, unfortunately, there is no way to make RegExpressValidator by default check for empty input fields.

0
source

Source: https://habr.com/ru/post/1286300/


All Articles