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?
You can roll your own CustomValidator, combining the functionality. Other than that, no not to my knowledge.
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.
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);
}
}
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.