有關Jquery Validator的使用

來源:互聯網
上載者:User

標籤:jquery   正則   javascript   .net   string   

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="WebApplication1.index" %>


<!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>
    <!--css,可以改動的,根據從www.jquery.com上面下載的外掛程式改動的-->
    <link href="css/screen.css" rel="stylesheet" type="text/css" />
    <script src="lib/jquery.js" type="text/javascript"></script>
    <script src="dist/jquery.validate.min.js" type="text/javascript"></script>
    <!--資訊漢化-->
    <script src="src/localization/messages_zh.js" type="text/javascript"></script>
    <style type="text/css">
        .style1
        {
            width: 50%; border:1px solid #cccccc; border-collapse:collapse; font-size:12px;
        }
        .style1 td{ border:1px solid #cccccc; height:25px; }
        input{ border:1px solid #cccccc; height:20px;}
        #Select1
        {
            height: 20px;
            width: 150px;
        }
      .success {
background:url("images/checked.gif") no-repeat 0px 0px;
padding-left: 16px;
        }
    </style>
    <script type="text/javascript">
        //自訂方法
        //addMethod("驗證方法名")
        $.validator.addMethod("checkHometown", function (value, element) {
            return value == "請選擇籍貫" ? false : true;
        }, "請選擇籍貫");
        //驗證手機號碼的自訂方法
        $.validator.addMethod("checkMobile", function (value, element) {
            var myreg = /^(((13[0-9]{1})|159|153)+\d{8})$/; //手機號碼正則
            if (myreg.test(value)) {
                return true;
            }
            else {
                return false;
            }


        }, "手機格式不對");


        $(function () {
           
            $("#signupForm1").validate({
                rules: {
                    uname: { required: true, minlength: 3, maxlength: 10,
                        remote: "check_user_exists.ashx"   //調用ajax使用一般處理常式,做使用者驗證,參數自己幫你傳了,一般是參數名(控制項名)=值
                    },
                    password: { required: true, minlength: 3, maxlength: 20 },
                    confirmPassword: { required: true, equalTo: "#password" },
                    homeTown: { checkHometown: true },
                    email: { required: true },
                    mobile: { required: true, checkMobile: true }//checkMobile是上面自己定義的方法
                },
                success: function (label) {
                    label.text(" ").addClass("success"); //驗證成功
                },
                messages: {
                    uname: { remote: "使用者名稱不可用" },
                    password: { required: "請輸入密碼", minlength: "密碼長度不少於3個", maxlength: "密碼長度不多於20" },
                    confirmPassword: { equalTo: "密碼不匹配" }


                },
                submitHandler: function () {
                    alert("添加"); //提交到伺服器
                }
            });


        });




    </script>
</head>
<body>
    <form id="signupForm1" runat="server" >
    <h1>驗證架構Demo</h1>
    <div>
    
        <table class="style1">
            <tr>
                <td>
                    使用者名稱</td>
                <td>
                    <input id="uname" name="uname" type="text" /></td>
            </tr>
            <tr>
                <td>
                    密碼</td>
                <td>
                    <input id="password" name="password" type="password" /></td>
            </tr>
            <tr>
                <td>
                    確認密碼</td>
                <td>
                    <input id="confirmPassword" name="confirmPassword" type="password" /></td>
            </tr>
            <tr>
                <td>
                    籍貫</td>
                <td>
                    <select id="homeTown" name="homeTown">
                     <option value="請選擇籍貫">請選擇籍貫</option>
                        <option value="湖南">湖南</option>
                        <option value="湖北">湖北</option>
                        <option value="江西">江西</option>
                    </select></td>
            </tr>
            <tr>
                <td>
                    Email</td>
                <td>
                    <input id="email" type="email" name="email" /></td>
            </tr>
            <tr>
                <td>
                    手機號碼</td>
                <td>
                    <input id="mobile" name="mobile" type="text"/></td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                   <input  type="submit" value="Submit"/>
                   </td>
            </tr>
        </table>
    
    </div>
    
    </form>
</body>

</html>


原始碼不知道怎麼上傳,有需要的可以聯絡我:[email protected]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.