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\"" + "}"; }