C# 如何结合RegularExpressionValidator控件和RequiredFieldValidator?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1002156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to combine RegularExpressionValidator control and RequiredFieldValidator?
提问by Dan Bailiff
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?
有没有办法把这两个控件结合起来,这样我就不用输入那么多代码了?
采纳答案by Ian Jacobs
You can roll your own CustomValidator, combining the functionality. Other than that, no not to my knowledge.
您可以推出自己的 CustomValidator,结合功能。除此之外,据我所知,没有。
回答by Genady Sergeev
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:
更具体地说,您需要将 CustomValdiator 的 ValidateEmptyMessage 属性设置为 true,否则他将不会验证空输入字段。例子:
<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 pointin 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.
但是,正如您所看到的,这绝不会比您以前的代码短,所以如果由我决定,在这种情况下使用自定义验证器是没有意义的。至于最初的问题,不幸的是没有办法让默认的 RegExpressValidator 验证空输入字段。
回答by Gagarin
You can override EvaluateIsValidmethod
您可以覆盖EvaluateIsValid方法
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);
}
}
回答by Johncl
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.
但它并没有解决同时触发多个验证器仍然会连续显示许多验证器错误的问题。自定义验证器可能是最好的解决方案。