Asp.Net 服務器控件下使用Jquery.validate.js

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

        <link href="/App_Themes/Default/style.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.js" type="text/javascript"></script>
</head>
<body>
<form id="aspnetForm" runat="server">
    <div>
    
    年齡:<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
        
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="須要驗證" />        
        &nbsp;&nbsp;        
        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="無須驗證" />
    
    </div>
    
    </form>


    <script type="text/javascript">
        //綁定表單驗證
        function NeedValidate() {
            $("#aspnetForm").validate({
                rules: {
                    txtAge: "required"
                },
                messages: {
                    txtAge: "名稱不能爲空!"
                }
            });
        }
        //取消表單驗證
        function NoValidate() {
            $("#txtAge").rules("remove"); //若是有多個 依次取消            
        }

        $(function () {
            //給須要驗證的控件爲表單綁定驗證
            $("#Button1").click(function () {
                NeedValidate();
            });

            //不須要驗證的控件 取消驗證綁定
            $("#Button2").click(function () {
                NoValidate();
            });
        });
</script>

</body>
</html>
相關文章
相關標籤/搜索