Jquery .validate.min的用法
- </head>
- <body>
- <form id="cerform" runat="server">
- <div>
- <span class="span9"><b>*</b>用戶真實姓名</span><input id="txtUserName" runat="server" class="input1 ipt_txt"
- type="text" />
- <span class="span10">請輸入您的真實姓名</span></div>
- <div class="clear height20">
- </div>
- <div>
- <span class="span9"><b>*</b>身份證號碼</span><input id="txtUserCardID" runat="server"
- class="input1 input2 ipt_txt" type="text" /><span class="span10">請輸入您的身份證號碼</span></div>
- <div class="clear height20">
- </div>
- <div>
- <span class="span9"><b>*</b>認證說明</span><textarea id="txtAreaContent" runat="server"></textarea><span class="span10">請完善認證說明,成功認證後,將出如今您的認證說明介紹中</span></div>
- <div class="clear height20">
- </div>
- <div>
- <span class="span9"><b>*</b>聯繫郵箱</span><input id="txtEmail" class="input1 ipt_txt" type="text" runat="server"/><span
- class="span10">請輸入您的電子郵箱</span></div>
- <div class="clear height20">
- </div>
- <div>
- <span class="span9"><b>*</b>手機號碼</span><input id="txtPhone" class="input1 ipt_txt" type="text" runat="server"/><span
- class="span10">請輸入能聯繫上您的手機號碼</span></div>
- <div class="clear height20">
- </div>
- <div>
- <span class="span9"></span>
- <input type="submit" class="button_a" value="提交認證" /></div>
- </form>
- </body>
- </html>
js:css
- jQuery.validator.addMethod("notEqualTo", function (value, element, param) {
- return value != $(param).val();
- }, $.validator.format("兩次輸入不能相同!"));
-
-
- jQuery.validator.addMethod("isNum", function (value, element) {
- var RegExp = /^\d+$/;
- return RegExp.test(value);
- }, $.validator.format("只能爲數字!"));
-
-
- $.validator.addMethod("buga", function (value) {
- return value == "buga";
- }, 'Please enter "buga"!');
-
-
- $.validator.addMethod("chinese", function (value, element) {
- var chinese = /^[\u4e00-\u9fa5]+$/;
- return (chinese.test(value)) || this.optional(element);
- }, "只能輸入中文");
-
-
- jQuery.validator.addMethod("byteRangeLength", function (value, element, param) {
- var length = value.length;
- for (var i = 0; i < value.length; i++) {
- if (value.charCodeAt(i) > 127) {
- length++;
- }
- }
- return this.optional(element) || (length >= param[0] && length <= param[1]);
- }, $.validator.format("請確保輸入的值在{0}-{1}個字節之間(一箇中文字算2個字節)"));
-
-
- 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));
-
-
- }, "請正確填寫您的聯繫電話");
-
-
- jQuery.validator.addMethod("isZipCode", function (value, element) {
- var tel = /^[0-9]{6}$/;
- return this.optional(element) || (tel.test(value));
- }, "請正確填寫您的郵政編碼");
-
-
-
-
- jQuery.validator.addMethod("string", function (value, element) {
- return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
- }, "不容許包含特殊符號!");
-
-
- jQuery.validator.addMethod("begin", function (value, element, param) {
- var begin = new RegExp("^" + param);
- return this.optional(element) || (begin.test(value));
- }, $.validator.format("必須以 {0} 開頭!"));
-
-
- jQuery.validator.addMethod("notEqualTo", function (value, element, param) {
- return value != $(param).val();
- }, $.validator.format("兩次輸入不能相同!"));
-
-
- jQuery.validator.addMethod("notEqual", function (value, element, param) {
- return value != param;
- }, $.validator.format("輸入值不容許爲{0}!"));
-
-
- jQuery.validator.addMethod("gt", function (value, element, param) {
- return value > param;
- }, $.validator.format("輸入值必須大於{0}!"));
-
-
- jQuery.validator.addMethod("decimal", function (value, element) {
- var decimal = /^-?\d+(\.\d{1,2})?$/;
- return this.optional(element) || (decimal.test(value));
- }, $.validator.format("小數位數不能超過兩位!"));
-
-
- jQuery.validator.addMethod("alnum", function (value, element) {
- return this.optional(element) || /^[a-zA-Z0-9]+$/.test(value);
- }, "只能包括英文字母和數字");
-
-
- jQuery.validator.addMethod("chcharacter", function (value, element) {
- var tel = /^[\u4e00-\u9fa5]+$/;
- return this.optional(element) || (tel.test(value));
- }, "請輸入漢字");
-
-
- jQuery.validator.addMethod("isIdCardNo", function (value, element) {
- return this.optional(element) || /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/.test(value) || /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$/.test(value);
- }, "請正確輸入您的身份證號碼");
-
-
- jQuery.validator.addMethod("isMobile", function (value, element) {
- var length = value.length;
- var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
- return this.optional(element) || (length == 11 && mobile.test(value));
- }, "請正確填寫您的手機號碼");
-
-
- jQuery.validator.addMethod("isTel", function (value, element) {
- var tel = /^\d{3,4}-?\d{7,9}$/;
- return this.optional(element) || (tel.test(value));
- }, "請正確填寫您的電話號碼");
-
-
- $(function () {
- $('#cerform').validate({
- rules: {
- txtUserName: {
- required: true,
- maxlength: 8,
- minlength: 4
- },
- txtUserCardID: {
- required: true,
- maxlength: 18,
- minlength: 18,
- isIdCardNo: "#txtUserCardID"
- },
- txtUserPwd: {
- required: true,
- maxlength: 18,
- minlength: 6,
- notEqualTo: "#txtUserPwd"
- },
- txtRUserPwd: {
- required: true,
- equalTo: "#txtUserPwd"
- },
- txtAddress: {
- required: true,
- maxlength: 50
- },
- txtAreaContent: {
- required: true,
- maxlength: 300
- },
- txtUrl: {
- required: true,
- url: true
- },
- txtPhone: {
- required: true,
- minlength: 7,
- maxlength: 13,
- isTell: true,
- isMobile: "#txtPhone"
- },
- txtZipCode: {
- required: true,
- minlength: 6,
- maxlength: 6,
- isNum: true
- },
- txtEmail: {
- required: true,
- email: true,
- maxlength: 40
- }
- },
- messages: {
- txtUserName: {
- required: "用戶名不能爲空!",
- maxlength: "最長爲18個字符!",
- minlength: "最短爲4個字符!",
- remote: "該用戶名已被佔用!"
- },
- txtUserCardID: {
- required: "身份證不能爲空!",
- maxlength: "身份證爲18個字符!",
- minlength: "身份證爲18個字符!"
- },
- txtUserPwd: {
- required: "密碼不能爲空!",
- maxlength: "最長爲18個字符!",
- minlength: "最短爲6個字符!",
- notEqualTo: "用戶名和密碼不能相同!"
- },
- txtRUserPwd: {
- required: "密碼不能爲空!",
- equalTo: "兩次輸入密碼不相同!"
- },
- txtAddress: {
- required: "地址不能爲空!",
- maxlength: "最長50個字符!"
- },
- txtAreaContent: {
- required: "不能爲空!",
- maxlength: "最長300個字符!"
- },
- txtUrl: {
- required: "網址不能爲空!",
- url: "請填寫正確的網址!"
- },
- txtPhone: {
- required: "電話不能爲空!",
- minlength: "最少7個數字!",
- maxlength: "最長13個數字!",
- isTell: "電話格式不正確!"
- },
- txtZipCode: {
- required: "郵編不能爲空!",
- minlength: "郵編爲6個數字!",
- maxlength: "郵編爲6個數字!",
- isNum: "請輸入數字!"
- },
- txtEmail: {
- required: "郵箱不能爲空!",
- email: "郵箱格式不正確!",
- maxlength: "最長40個字符!"
- }
- },
- errorPlacement: function (error, element) {
- error.appendTo(element.parent());
- },
- submitHandler: function (form) {
- form.submit();
- },
- errorClass: "error",
- focusCleanup: true,
- success: function (label) {
- label.html("<span style=\"color:green\">填寫正確!</span>").addClass("success");
- }
- });
-
-
-
-
-
- $(".ipt_txt").focus(function () {
- $(this).css("background-color", "#FFFFCC").blur(function () {
- $(this).css("background-color", "#FBFBFB");
- });
- });
- });
歡迎關注本站公眾號,獲取更多信息