ASP.Net防止同时触发比较验证器和正则表达式验证器 [英] ASP.Net Prevent compare validator and regular expression validator from firing at the same time

查看:56
本文介绍了ASP.Net防止同时触发比较验证器和正则表达式验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个这样的文本框:

I've got two text boxes like this:

<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" />

<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." />

这是两个带有正则表达式验证器的文本框,这些验证器将它们限制为仅允许正实数.这些工作正常.

It's two text boxes that have regular expression validators that constrain them to only allow positive real numbers. These work fine.

我还希望第二个文本框中的输入小于第一个文本框中的输入.为此,我有一个比较验证器.

I also want the input in the second text box to be smaller than the first one. For that I have a compare validator.

当用户输入正确的数字时,比较验证器就可以正常工作.

When the user has correct numbers the compare validator works fine.

当他们输入的任何内容都使第二个正则表达式验证器失败时,比较验证器也会同时触发.

It's when they enter in anything that fails the second regular expression validator, that the compare validator also fires at the same time.

第一个文本框中的内容,有效输入,错误输入甚至什么都没有关系.两个第二个验证器都失败.

It doesn't matter what's in the first text box, valid input, wrong input, or even nothing. Both of the second validators fail.

即使验证者应该比较双打.

Even though the validator is supposed to be comparing doubles.

对此有一个简单的解决方法吗?

Is there an easy fix for this?

我意识到这种行为是好的,因为验证器在应有的时候是无效的,但是用户会看到不正确的错误消息.

I realize that this behavior is okay, because a validator is invalid when it should be, but the user would be seeing the incorrect error messages.

我已经做过一个涉及自定义验证器和Javascript的解决方案,如果涉及到它,那么我将不得不再次做.但是,如果是这种情况,那么使用比较验证器就没有多大意义,因为它永远不会与正则表达式验证器一起使用.

I've already done a solution involving custom validators and Javascript, and if it comes down to it then I'll have to do that again. But if that's the case then there's not much point to using a compare validator, since it'll never work with a regular expression validator.

推荐答案

将不同的验证组分配给正则表达式验证器并比较验证器,并使用ASP.NET内置的javascript函数 Page_ClientValidate()检查验证之一一个.

Assign different validation groups to regular expression validator and compare validator and use ASP.NET inbuilt javascript function Page_ClientValidate() to check validation one by one.

<asp:TextBox ID="textBox1" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox1" runat="server" ControlToValidate="textBox1" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2"/>

<asp:TextBox ID="textBox2" runat="server" />
<asp:RegularExpressionValidator ID="regExTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Not a positive real number." Display="Dynamic" ValidationExpression="(^0*[1-9]+\d*(\.\d+)?$)|(^0*\.0*[1-9]+\d*$)" ValidationGroup="Group1" />
<asp:RequiredFieldValidator ID="reqFldTextBox2" runat="server" ControlToValidate="textBox2" ErrorMessage="Enter a number." Display="Dynamic" ValidationGroup="Group2" />
<asp:CompareValidator id="compareValidator" runat="server" ControlToValidate="textBox2" ControlToCompare="textBox1" Type="Double" Display="Dynamic" Operator="LessThan" Text="Error." ValidationGroup="Group3" />

<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return Validate()" />

使用Java语言

    <script type="text/javascript">
        function Validate() {
            var isValid = false;
            isValid = Page_ClientValidate('Group1');
            if (isValid) {
                isValid = Page_ClientValidate('Group2');
            }
            if (isValid) {
                isValid = Page_ClientValidate('Group3');
            }
            return isValid;
        }
    </script>

这篇关于ASP.Net防止同时触发比较验证器和正则表达式验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆