1. 程式人生 > >jquery.validate 驗證記錄

jquery.validate 驗證記錄

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>個人中心_基本資訊</title>
    <link href="../css/Member.css" rel="stylesheet" type="text/css" />
    <link href="../css/base.css" rel="stylesheet" type="text/css" />
    <script src="../js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../js/jquery.validate.js" type="text/javascript"></script>
    <script type="text/javascript">
        function checkidcard(num) {
            var len = num.length, re;
            if (len == 15)
                re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
            else if (len == 18)
                re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
            else {
                //alert("請輸入15或18位身份證號,您輸入的是 "+len+ "位");   
                return false;
            }
            var a = num.match(re);
            if (a != null) {
                if (len == 15) {
                    var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                } else {
                    var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
                    var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
                }
                if (!B) {
                    //alert("輸入的身份證號 "+ a[0] +" 裡出生日期不對!");   
                    return false;
                }
            }

            return true;
        }   
    </script>
    <script type="text/javascript">
        /*  **/
        // 新增驗證方法 (身份證號碼驗證)  
        jQuery.validator.addMethod("isIdCardNo", function (value, element) {
            return this.optional(element) || checkidcard(value);
        }, "請正確輸入您的身份證號碼");

        //聯絡電話(手機/電話皆可)驗證   
        jQuery.validator.addMethod("isPhone", function (value, element) {
            var length = value.length;
            var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
            var tel = /^\d{3,4}-?\d{7,9}$/;
            return this.optional(element) || (tel.test(value) || mobile.test(value));

        }, "請正確填寫您的聯絡電話");

        $(document).ready(function () {
            $(".hy_lesson_nav_list a").removeClass();
            $("#infor").attr("class", "hy_nav_enter_current");







            $("#form1").validate({
                rules: {
                    txtUserName: {
                        required: true,
                        rangelength: [2, 20],
                        remote: { //驗證使用者名稱是否存在
                            url: "../Handler/StudentInfor.ashx", //後臺處理程式    
                            type: "post",  //資料傳送方式   
                            dataType: "json",       //接受資料格式       
                            data: { key: "checknewname", newname: function () { return jQuery("#<%=txtUserName.ClientID %>").val(); }, oldname: $.trim($("#hidUserName").val())} //要傳遞的資料   
                        }
                    },
                    txtNikeName: {
                        required: true,
                        rangelength: [2, 20]
                    },
                    txtRealName: {
                        required: true,
                        rangelength: [2, 4]
                    },
                    txtCardNo: {
                        required: true,
                        isIdCardNo: true
                    },
                    txtEmail: {
                        required: true,
                        email: true
                    },
                    txtTel: {
                        required: true,
                        isPhone: true
                    }, txtAge: {
                        required: true,
                        digits: true
                    }
                },
                messages: {
                    txtUserName: {
                        required: "請輸入使用者名稱",
                        rangelength: "長度在2位至20位",
                        remote: "使用者名稱已被註冊"
                    },
                    txtNikeName: {
                        required: "請輸入暱稱",
                        rangelength: "長度在2位至20位"
                    },
                    txtRealName: {
                        required: "請輸入真實姓名",
                        rangelength: "長度在2位至4位"
                    },
                    txtCardNo: {
                        required: "請輸入身份證號碼",
                        isIdCardNo: "請正確輸入您的身份證號碼"
                    },
                    txtEmail: {
                        required: "請輸入電子郵箱",
                        email: "請正確輸入電子郵箱"
                    },
                    txtTel: {
                        required: "請輸入聯絡電話",
                        isPhone: "請正確輸入聯絡電話"
                    },
                    txtAge: {
                        required: "請輸入年齡",
                        digits: "必須輸入整數"
                    }
                }
            });


            //調取所在地區
            //GetZone();
        });
        function GetZone() {
            var father_id = $.trim($("#ddlCity").val());
            if (father_id != "") {
                $.getJSON("../Handler/StudentInfor.ashx", { key: "area", father_id: father_id },
                            function (json) {
                                var options = '';
                                options += "<option value=''>--縣/區--</option>";
                                $.each(json.result, function (i, item) {
                                    options += "<option value='" + item.id + "'>" + item.name + "</option>";
                                });
                                $('#ddlZone').html(options);
                            });
            }
        }



    </script>
    <style type="text/css">
        #form1 label.error
        {
            color: red;
            font-family: "Microsoft YaHei" !important;
            margin-left: 50px;
            width: auto;
            display: inline;
            font-size: 12px;
            margin-left: -222px;
        }
    </style>
</head>
<body class="xk_body_bg">
    <form id="form1" runat="server">
    <!--頭部 -->
    <uc1:MemberTop ID="MemberTop1" runat="server" />
    <div class="hy_main">
        <div class="hy_lesson_main clearfix">
            <!--帳號設定開始 -->
            <uc3:AccountSettings ID="AccountSettings1" runat="server" />
            <!--帳號設定結束 -->
            <div class="hy_information_main clearfix">
                <asp:HiddenField ID="hidUserName" runat="server" />
                <div class="hy_information_main_title">
                    <h2>
                        基本資訊</h2>
                </div>
                <div class="hy_information_main_list clearfix">
                    <table width="699" height="410" border="0" cellpadding="0" cellspacing="0">
                        <tbody>
                            <tr>
                                <td align="right" valign="top">
                                    使用者名稱:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtUserName" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                                <td align="right" valign="top">
                                    暱 稱:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtNikeName" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    真實姓名:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtRealName" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                                <td align="right" valign="top">
                                    性別:
                                </td>
                                <td align="left" valign="top">
                                    <asp:RadioButtonList ID="rbSex" CssClass="hy_information_main_radio" runat="server"
                                        RepeatDirection="Horizontal" RepeatLayout="Flow">
                                        <asp:ListItem Selected="True" Value="1">男</asp:ListItem>
                                        <asp:ListItem Value="0">女</asp:ListItem>
                                    </asp:RadioButtonList>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    身份證號:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtCardNo" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                                <td align="right" valign="top">
                                    學歷:
                                </td>
                                <td align="left" valign="top">
                                    <asp:DropDownList ID="ddlEducation" runat="server">
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    民族:
                                </td>
                                <td align="left" valign="top">
                                    <asp:DropDownList ID="ddlNation" runat="server">
                                    </asp:DropDownList>
                                </td>
                                <td align="right" valign="top">
                                    年齡:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtAge" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    來源:
                                </td>
                                <td align="left" valign="top">
                                    <asp:DropDownList ID="ddlSource" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow">
                                    </asp:DropDownList>
                                </td>
                                <td align="right" valign="top">
                                    Q Q:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtQQ" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    聯絡電話:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtTel" CssClass="hy_information_main_text" runat="server"></asp:TextBox>
                                </td>
                                <td align="right" valign="top">
                                    郵箱:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtEmail" runat="server" CssClass="hy_information_main_text"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    <span style="width: 238px;">所在地區:
                                </td>
                                <td align="left" valign="top">
                                    <asp:DropDownList ID="ddlCity" runat="server" onchange="javascript:GetZone();">
                                    </asp:DropDownList>
                                       
                                    <asp:DropDownList ID="ddlZone" runat="server">
                                    </asp:DropDownList>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    現住地址:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtAddress2" runat="server" CssClass="hy_information_main_text"></asp:TextBox>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    學習/工作單位:
                                </td>
                                <td align="left" valign="top">
                                    <asp:TextBox ID="txtWorkUnits" runat="server" CssClass="hy_information_main_text"></asp:TextBox>
                                </td>
                                <td>
                                </td>
                                <td>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
                <div class="hy_information_btn">
                    <asp:Button ID="btnSave" CssClass="btnSaveCss" runat="server" Text="確定" OnClick="btnSave_Click" />
                </div>
            </div>
        </div>
    </div>
    <!--底部--->
    <uc2:MemberFooter ID="MemberFooter1" runat="server" />
    </form>
</body>
</html>

插入.ashx 驗證

public string CheckNewUserName()
        {
            try
            {
                string newname = CommonClass.Request.GetRequest<string>("newname", "");
                string oldname = CommonClass.Request.GetRequest<string>("oldname", "");
                //DataTable dt = Query.ProcessSql("select id from tabStudent where UserName!='chinaboykai' and UserName='chinaboy'", Common_CRM.PublicDAL.DbName);
                string sql = string.Format("select id from tabStudent where UserName!='{0}' and UserName='{1}'", oldname, newname);
                DataTable dt = Query.ProcessSql(sql, Common_CRM.PublicDAL.DbName);
                StringBuilder json = new StringBuilder();
                if (!CommonClass.DTRow.CheckDtIsEmpty(dt))
                {
                    //return "{" + "\"result\":\"true\"" + "}";
                    return "false";
                }
                else
                {
                    //return "{" + "\"result\":\"false\"" + "}";
                    return "true"; // return "{" + "\"false\"" + "}";
                }

            }
            catch (Exception ex)
            {
                AppLog.Write("驗證使用者名稱![異常資訊:" + ex.Message + "]", AppLog.LogMessageType.Info);
                return string.Empty;
            }
           // return "{" + "\"result\":\"false\"" + "}";

        }