0

我在 TextBox 上有一个RequiredFieldValidator。当在 TextBox 中没有输入任何内容时,这可以正常工作。现在,我要做的另一项验证是,当用户输入一些垃圾数据时,我会抛出一条错误消息,说“输入无效”。这是在标签上。

现在的场景是在抛出错误消息之后,如果用户清空文本框并单击按钮,RequiredFieldValidator 工作但标签上的错误消息仍然保持原样。一旦用户清空文本框,我想隐藏/删除它。

为此,我使用了 JavaScript 函数,但RequiredFieldValidator 不起作用。这是我的代码:

<asp:TextBox ID="txtemp" runat="server"></asp:TextBox>
<asp:Button ID="btnstatus" runat="server"  ValidationGroup="valgrp1" OnClientClick="Validate()"
    CausesValidation="true"  onclick="btnstatus_Click" 
    Text="Fetch status message" BackColor="#ccebff" />
&nbsp;
<asp:RequiredFieldValidator ID="Reqfield1" ControlToValidate="txtportalid"  ValidationGroup="valgrp1" ErrorMessage="wrong entry" runat="server" />
</div>
<div>
    <asp:Label ID="lblerrormsg" runat="server"  Font-Bold="true"  Visible="false" ForeColor="#FF3300">
    </asp:Label>
</div>

JavaScript:

function Validate()
{
    var txt1 = document.getElementById("<%= Txtemp.ClientID %>");
    var val1 = txt1.value.replace(/\s/g, "");

    if (val1=="")
    {
        document.getElementById("<%= lblerrormsg.ClientID%>").style.display = 'none';
    }
}
4

2 回答 2

3

我建议使用带有属性集的CustomValidatorClientValidationFunction来指向您的Validate函数,如下所示:

<script>
    function Validate(source, arguments) {
        arguments.IsValid = /* your validation logic */
    }
</script>

...

<asp:CustomValidator ControlToValidate="txtportalid" 
    ErrorMessage="..." ClientValidationFunction="Validate" runat="server" />

...或者,在您的情况下,您可以只使用RegularExpressionValidator。两者都会给你你正在寻找的行为。

于 2013-01-15T14:18:22.117 回答
0

You could handle the keyup event for the textbox using jQuery.

The example below does two things:

  1. Everytime a keypress occurs on the textbox check whether the textbox is empty and
  2. Check whether the error label has a value

If both the checks have passed, simply clear the error label!

ASPX:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Home</title>
    <script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#txtemp").keyup(function () {
                if (!this.value) {
                    var errorMessage = $("#<%= lblErrorMessage.ClientID %>").length;
                    if (errorMessage) {
                        $("#<%= lblErrorMessage.ClientID %>").html("");
                    }
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="txtemp" runat="server" />
        <asp:Button ID="btnstatus" runat="server" ValidationGroup="valgrp1" CausesValidation="true"
            OnClick="btnstatus_Click" Text="Fetch status message" />
        <asp:RequiredFieldValidator runat="server" ID="req" ValidationGroup="valgrp1" ControlToValidate="txtemp"
            ErrorMessage="Field is required" />
        <asp:Label ID="lblErrorMessage" ClientIDMode="Static" runat="server" EnableViewState="false" />
    </div>
    </form>
</body>
</html>

Code behind (I've just created a simple method for testing) :

protected void btnstatus_Click(object sender, EventArgs e)
{
    if (txtemp.Text == "invalid")
    {
        lblErrorMessage.Text = "The data is invalid";
    }
}
于 2013-01-15T17:29:54.747 回答