必填字段检入asp.net文本框

时间:2012-03-07 01:05:51

标签: c# asp.net

如何实现包含以下文本的clientidevalidation必需文本字段:

[姓名] [需要的地址]

我希望尽可能使用asp:CustomValidator?只有当两个字段都有数据时才会触发回发?

     <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">        var $j = jQuery.noConflict();</script>
    <script type="text/javascript">


        function otherMessageValidator_ClientValidation(source, args) {
            args.IsValid = false;

            var nm = $j("#name");

            if (nm.val() != "" && nm.val() != "[Name required]") {
                args.IsValid = true;
            }

            return args.IsValid;
        };
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="name" runat="server" ValidationGroup="valgroup">[Name required]</asp:TextBox>
        <asp:TextBox ID="address" runat="server" ValidationGroup="valgroup">[Address required]</asp:TextBox>
    </div>
    <asp:Button runat="server" ID="but1" Text="go" OnClick="but1_Click" />
    <asp:CustomValidator ID="MyCustomValidator" runat="server" ValidationGroup="valgroup"
        ClientValidationFunction="otherMessageValidator_ClientValidation" ErrorMessage="At least one textbox needs to be filled in." />
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

实现它的一种方法是使用javascript中的正则表达式并仅在客户端验证它。因此,在提到的代码中,如果满足所有必需的验证,它将只进入服务器,否则它将在摘要div标签的屏幕上显示错误消息。

以下是必需的代码。

 <script type="text/javascript">               
 function validateFields() {

                var message = "";
                var error = false;

               var name = document.getElementById("<%=txtname.ClientID %>");
                var address = document.getElementById("<%=txtaddress.ClientID %>");

                //work as required field validation
                if ((/\S+/.test(name.value)) == false) {

                    message += "Enter Name" + "<br>";
                    error = true;
                }
                //work as required field validation
                if ((/\S+/.test(address.value)) == false) {

                    message += "Enter Address" + "<br>";
                    error = true;
                }


                if (error) {
                        document.getElementById("validationSummary").innerHTML = message;
                        document.getElementById("validationSummary").style.display = "block";
                        return false;
                    } else {
                        document.getElementById("validationSummary").innerHTML = "";
                        document.getElementById("validationSummary").style.display = "none";
                        return true;
                    }


                }
 </script>

我们需要一个div来显示validationsummary和按钮

<div id="validationSummary" style="display: none; float: left">
                                </div>
  <asp:LinkButton ID="lnkAddNew" runat="server" OnClientClick="return validateFields()" OnClick="lnkAddNewPatient_Click" >Save</asp:LinkButton>

希望它能解决您的问题