ASP.Net 的 NumericValidator 控件






3.57/5 (3投票s)
2007 年 11 月 13 日
2分钟阅读

32789

350
Numeric Validator 是一个控件,可执行必填字段、小数和范围验证

引言
.Net Framework 中提供的验证控件几乎可以满足应用程序的所有基本验证需求。复杂的验证需求可以通过组合两个或多个提供的验证控件来实现。其中一种验证是处理数字的 Textbox 控件。数字验证包括以下内容。
1. 必填字段验证
2. 小数位数验证
3. 范围验证
每当应用程序需要此功能时,开发人员需要为每个文本框控件使用上述验证器控件。为了避免重复性工作,我设计了一个控件,可以在一个控件中执行所有这些验证。
控件内部的验证是使用 JavaScript 执行的。最重要的是,我正在使用 .Net Framework 中现有的 JavaScript 方法 - 所以“无需重复发明轮子”。但是,处理这些方法的逻辑是我的。
背景
我参与提供银行解决方案的项目。 大部分时间我都在处理文本框和数字验证。 我发现自己正在做同样的重复验证,这很耗时。 因此,我结合了一些常规的数字验证,并将其制作成一个单独的控件。
NumericValidator 控件的描述
如前所述,NumericValidator 控件派生自 CustomValidator 类。 它具有以下属性。 AllowDecimalErrorMessage
- 当验证的控件具有小数值时,在 ValidationSummary 中显示的消息。RangeValidationErrorMessage
- 当验证的控件值不在指定的最大值和最小值范围内时,在 ValidationSummary 中显示的消息。MaximumValue
- 要验证的控件的最大值。MinimumValue
- 要验证的控件的最小值。InitialValue
- 要验证的字段的初始值。AllowDecimal
- 指示要验证的控件是否允许使用小数。RequiredFieldErrorMessage
- 当验证的控件为空时,在 ValidationSummary 中显示的消息。RequiredFieldValidator
- 指示是否检查要验证的控件是否为必填字段。DecimalValidator
- 指示是否验证要验证的控件的小数值。EnableOnControlToValidateStatus
- 验证器控件仅在 Control To Validate 启用时启用
在 OnPreRender
方法中实现的逻辑以实现该功能
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
script = new StringBuilder();
base.ClientValidationFunction = "MaxNumericValidator";
base.ValidateEmptyText = this.RequiredFieldValidator;
if (!Page.ClientScript.IsClientScriptBlockRegistered("MaxNumericValidatorScript"))
{
script.Append("<script type=\"text/javascript\">\n");
script.Append("function MaxNumericValidator(source, args)\n");
script.Append("{\n");
script.Append("\tif(source.enableOnControlToValidateStatus == \"True\")\n");
script.Append("\t{\n");
script.Append("\t\tif(document.all[source.controltovalidate].disabled == true)\n");
script.Append("\t\t{\n");
script.Append("\t\t\targs.IsValid = true;\n");
script.Append("\t\t\treturn;\n");
script.Append("\t\t}\n");
script.Append("\t}\n");
script.Append("\tvar value = ValidatorGetValue(source.controltovalidate);\n");
script.Append("\tif(source.requiredfieldvalidator == \"True\")\n");
script.Append("\t{\n");
script.Append("\t\tif((ValidatorTrim(value) != ValidatorTrim(source.initialvalue)) == false)\n");
script.Append("\t\t{\n");
script.Append("\t\t\tsource.innerHTML = source.requiredfielderrormessage;\n");
script.Append("\t\t\targs.IsValid = false;\n");
script.Append("\t\t\treturn\n");
script.Append("\t\t}\n");
script.Append("\t}\n");
script.Append("\tif (ValidatorTrim(value).length == 0)\n");
script.Append("\t{\n");
script.Append("\t\targs.IsValid = true;\n");
script.Append("\t}\n");
script.Append("\tvar strRegExp;\n");
script.Append("\tif(source.decimalValidator == \"True\")\n");
script.Append("\t{\n");
script.Append("\t\tif(source.allowdecimal == 0)\n");
script.Append("\t\t\tstrRegExp = " + @"""^[-+]?[0-9]\\d*"";" + "\n");
script.Append("\t\telse\n");
script.Append("\t\t\tstrRegExp = " + @"""^[-+]?[0-9]\\d*(\\.\\d{1,"" + source.allowdecimal + ""})?%?$"";" + "\n");
script.Append("\t\tvar rx = new RegExp(strRegExp);\n");
script.Append("\t\tvar matches = rx.exec(value);\n");
script.Append("\t\tsource.innerHTML = source.allowdecimalerrormessage;\n");
script.Append("\t\targs.IsValid = (matches != null && value == matches[0]);\n");
script.Append("\t}\n");
script.Append("\tif(args.IsValid == true && (source.maximumvalue != \"\" || source.minimumvalue != \"\"))\n");
script.Append("\t{\n");
script.Append("\t\tsource.innerHTML = source.rangevalidationerrormessage;\n");
script.Append("\t\targs.IsValid = (ValidatorCompare(value, source.minimumvalue, \"GreaterThanEqual\", source) && \n");
script.Append("\t\t\tValidatorCompare(value, source.maximumvalue, \"LessThanEqual\", source));\n");
script.Append("\t}\n");
script.Append("\tif(args.IsValid == true)\n");
script.Append("\t{\n");
script.Append("\t\tsource.innerHTML = \"\";\n");
script.Append("\t}\n");
script.Append("}\n");
script.Append("</script>\n");
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MaxNumericValidatorScript", script.ToString());
}
script = new StringBuilder();
if (!Page.ClientScript.IsStartupScriptRegistered(this.ClientID))
{
try
{
script.Append("<script type="\"text/javascript\""></script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID, script.ToString());
}
catch (Exception exp)
{
this.Page.Response.Write(exp.Message);
this.Page.Response.End();
}
}
}
使用代码
通过将 NumericValidator.dll 添加到工具箱,将控件添加到您的 Web 应用程序。 NumericValidator 控件将显示在“验证”选项卡下。 将 Textbox 控件、NumericValidator 控件和 Button 控件添加到网页。
将 NumericValidator 控件的 controltovalidate
属性设置为 Textbox 控件的名称。 为了实现所有 requiredfield、rangevalidation(10-100)和小数位验证(最多 2 位小数),请设置以下属性并执行应用程序
AllowDecimal="2"
DecimalValidator="True"
MaximumValue="100"
MinimumValue="10"
RequiredFieldValidator="True"
<cc1:numericvalidator id="NumericValidator1" runat="server" controltovalidate="TextBox1" AllowDecimal="2" DecimalValidator="True"
MaximumValue="100" MinimumValue="10" RequiredFieldValidator="True"></cc1:numericvalidator>