/** * Theme: Moltran Admin Template * Author: Coderthemes * Form Validator */ !function($) { "use strict"; var FormValidator = function() { this.$registerForm = $("#registerForm"), this.$loginForm = $("#loginForm"), this.$commentForm = $("#commentForm"); //this could be any form, for example we are specifying the comment form }; $.extend($.validator.messages, { required: "该输入项是必输项", email:"请输入有效的邮箱地址" }); jQuery.validator.addMethod("usernameCheck", function(value, element) { var workType; for (var i = 0; i < value.length; i++) { workType = value.charCodeAt(i); if ((workType >= 64 && workType <= 90) || (workType >= 97 && workType <= 122) || (workType >= 48 && workType <= 57) || (workType == 95) || (workType == 46)) {//ASCII码值判断为字母数字或下划线,邮箱 continue; } else {//不符合则输出提示 return false; } } return true; }, "用户名称应为字母、数字、下划线或邮箱"); //密码不能包括空格 jQuery.validator.addMethod("passwdBankCheck", function(value, element) { var workType; if(value.indexOf(" ") >= 0){ return false; }else{ return true; } }, "密码不能包含空格"); //init FormValidator.prototype.init = function() { //validator plugin $.validator.setDefaults({ /* submitHandler: function(form) { alert("submitted!"); } */ }); // validate the comment form when it is submitted this.$commentForm.validate(); // validate signup form on keyup and submit this.$loginForm.validate({ rules: { "user.username": { required: true, rangelength:[6,50] }, "user.password": { required: true, rangelength:[6,20] } }, messages: { "user.username": { required: "请输入用户名称", rangelength: "用户名称长度必须在{0}到{1}之间" }, "user.password": { required: "请输入登录密码", rangelength: "登录密码为{0}~{1}位字符,请重新输入!" } } }); //register validator this.$registerForm.validate({ rules: { "company.name": { required: true, maxlength: 60 }, "user.email": { required: true, email: true }, "user.username":{ required:true, rangelength: [6,50], usernameCheck:true }, "user.password": { required: true, rangelength: [6,20], passwdBankCheck:true }, "confirmpassword": { required: true, rangelength: [6,20], equalTo: "#password" }, "user.phone":{ required:true, maxlength:20 }, "verificationCode":{ required:true } }, "messages": { "company.name":{ required:"请填写公司名称" }, "user.email":{ required:"请输入邮箱地址", email:"请输入有效的邮箱地址" }, "user.username":{ required:"请输入用户名称", rangelength:"用户名称长度必须在6到50之间" }, "user.password":{ required:"请输入登录密码", rangelength:"登录密码为6~20位字符,请重新输入!" }, "confirmpassword": { required:"请输入确认密码", rangelength:"登录密码为6~20位字符,请重新输入!", equalTo:"两次输入密码不一致" }, "user.phone":{ required:"请输入手机号码!" } } }); }, //init $.FormValidator = new FormValidator, $.FormValidator.Constructor = FormValidator }(window.jQuery), //initializing function($) { "use strict"; $.FormValidator.init() }(window.jQuery);