ASP.NET RegularExpressionValidator 触发器 CompareValidator

标签 asp.net

基本上,我的代码如下所示:

<div>

  <asp:TextBox ID="txtBox1" class="inline-block" runat="server" />
  <asp:RegularExpressionValidator ID="regExTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
  <asp:RequiredFieldValidator ID="reqFldTxtBox1" runat="server" ControlToValidate="txtBox1" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />
  <asp:CompareValidator id="cmprTxtBox1" runat="server" ControlToValidate="txtBox1" ControlToCompare="txtBox2" Type="Double" Display="Dynamic" Operator="LessThanEqual" Text="Error: txtBox1 is greater than txtBox2." />

</div>

<br />

<div>

  <asp:TextBox ID="txtBox2" runat="server" />
  <asp:RegularExpressionValidator ID="regExTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Enter an integer or decimal greater than 0." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
  <asp:RequiredFieldValidator ID="reqFldTxtBox2" runat="server" ControlToValidate="txtBox2" ErrorMessage="Requires a number." Display="Dynamic" InitialValue="" />

</div>

我有两个使用正则表达式验证器的文本框,以强制用户仅输入整数或小数。我还希望 txtBox2 中的输入大于 txtBox1,因此我在 txtBox1 上使用比较验证器,以便检查 txtBox1 中的数字何时大于 txtBox2。

我遇到的问题是,当 RegularExpressionValidator 触发时,我的 CompareValidator 总是会触发。当 txtBox1 中有正常数字而 txtBox2 中没有任何数字时,CompareValidator 完全满足。但只要 RegularExpressionValidator 失败,无论 txtBox2 中有什么内容,它都会失败。

当然,系统运行正常,因为无论哪种方式,当验证器返回无效时,用户都无法输入任何值。但我不希望用户看到不必要的错误消息。

如果没有任何解决方案,我想我可以实现一个自定义验证器。

最佳答案

请使用验证组属性进行检查...... 对于验证控件,并将相同的验证组放入提交按钮,这样它将在单击按钮时进行验证...... 希望对您有帮助......

关于ASP.NET RegularExpressionValidator 触发器 CompareValidator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31952415/

相关文章:

c# - ASP.NET 如何在请求中获取自定义 header ?

c# - 将生成的pdf直接保存到服务器目录文件夹中,无需用户提示

asp.net - 在 ASP.NET 和 Javascript 中使用复选框

c# - 数据列表问题

c# - 如何在没有 Authorize 属性的 ASP Core 2 方法中获取用户声明?

c# - 从 PreviousPage.FindControl 获取值

c# - 从 ASP.NET 网站获取 IIS 站点名称

c# - ASP.Net DropDownList OnSelectedIndexChange 未触发

asp.net - 如何开发、测试、版本、同步SQL Server 2000、2005、2008、2008R2数据库?

asp.net - 允许预编译的 Visual Studio 网页可更新与不可更新之间有什么区别?