【问题标题】:compare three textbox in asp.net比较asp.net中的三个文本框
【发布时间】:2017-08-07 13:07:49
【问题描述】:

我有问题。我必须将一个文本框与其他两个文本框进行比较,如果值匹配,则它们应该显示一条错误消息。

我尝试了两个比较验证器,但一次只有一个比较验证器有效,所以获取第一个值只能查看我的代码。

<asp:CompareValidator ForeColor="Red" Font-Size="Small" runat="server" 
     ID="CompareValidator2" controltovalidate="txtsponsorfatherhusbandname" 
     controltocompare="txtfatherhusbandname" operator="NotEqual" type="String" 
     errormessage="Sponsor and applicant father name cannot be same" SetFocusOnError="true">
</asp:CompareValidator>
<asp:CompareValidator ForeColor="Red" Font-Size="Small" runat="server" 
     ID="cmpfather" controltovalidate="txtsponsorname" 
     controltocompare="txtfatherhusbandname" operator="NotEqual" type="String" 
     errormessage="Sponsor and applicant father name cannot be same" SetFocusOnError="true">
</asp:CompareValidator>         

我曾使用代码文件对其进行编码,但问题是它必须刷新页面,因此如果有任何帮助,将不胜感激。 see the picture i want to check if applicant name is not equal to either Sponsor name and sponsor father husband name and if they are equal then they show a message and focus is on applicant name

【问题讨论】:

    标签: c# jquery asp.net


    【解决方案1】:

    您可以为此使用CustomValidator。您可以在ClientValidationFunction 中编写自己的逻辑。

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <br />
    
    <asp:CustomValidator ID="CustomValidator1" runat="server" 
        ClientValidationFunction="checkAll3Boxes" ErrorMessage="All three are the same"></asp:CustomValidator>
    
    <script type="text/javascript">
        function checkAll3Boxes(sender, element) {
            var tb1 = document.getElementById('<%= TextBox1.ClientID %>').value;
            var tb2 = document.getElementById('<%= TextBox2.ClientID %>').value;
            var tb3 = document.getElementById('<%= TextBox3.ClientID %>').value;
    
            if (tb1 != "" && tb1 == tb2 && tb1 == tb3) {
                element.IsValid = false;
            } else {
                element.IsValid = true;
            }
        }
    </script>
    

    【讨论】:

    • 此脚本在语句 id 无效后未在文本框前显示任何消息
    • 我试过 sender.innerHTML = "你的 HTML 什么都来了"; document.getElementById('lbltipAddedComment').innerHTML = '您的小费已提交!';但是标签的值也没有改变
    • CustomValidator 的ErrorMessage 已显示。
    • 是的。如果不是,您有 javascript 错误。检查浏览器控制台。我已经更新了一个完整的例子。将其复制到空白页面,您会看到它有效。
    • 在空白页中工作,但在按钮单击时,但在文本框更改文本上工作我尝试过 autopostback="true" 但没有工作
    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2013-09-13
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    • 2015-10-04
    • 2015-09-24
    • 2013-05-19
    相关资源
    最近更新 更多