需要引入jquery.form.js
自定义方法:
// 匹配english
jQuery.validator.addMethod(“isEnglish”, function(value, element) {
return this.optional(element) || /^[A-Za-z]+$/.test(value);
}, “匹配english”);
提交:
var FormValidation = function () {
return {
//main function to initiate the module
init: function () {
// for more info visit the official plugin documentation:
// http://docs.jquery.com/Plugins/Validation
var form1 = $('#fromAddOrEdit');
var error1 = $('.alert-danger', form1);
var success1 = $('.alert-success', form1);
form1.validate({
errorElement: 'span', //default input error message container
errorClass: 'help-block', // default input error message class
focusInvalid: false, // do not focus the last invalid input
ignore: "",
rules: {
name: {
maxlength:200,
required: true,
remote:"existName.do"
},
roleid: {
maxlength:200,
required: true,
alnum: true,
},
description: {
maxlength:200,
required: true,
},
yhm: {
maxlength:200,
required: true
},
password:{
required: true,
maxlength:25
},
password2:{
required: true,
maxlength:25,
equalTo:"#password"
}
},
messages: { // custom messages
name: {
required: "请输入登录名",
remote: "该登录名已存在"
},
yhm: {
required: "请输入用户名"
},
roleid: {
alnum: "请输入正确的字母"
},
password: {
required: "请输入登录密码"
},
password2: {
required: "请输入确认密码",
equalTo: "两次密码不一致"
}
},
invalidHandler: function (event, validator) { //display error alert on form submit
success1.hide();
$('.alert-danger span.sei-form-message').text("请检查下列输入问题。");
error1.show();
},
highlight: function (element) { // hightlight error inputs
$(element)
.closest('.form-group').addClass('has-error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change done by hightlight
$(element)
.closest('.form-group').removeClass('has-error'); // set error class to the control group
},
success: function (label) {
label
.closest('.form-group').removeClass('has-error'); // set success class to the control group
},
submitHandler: function (form) {
success1.show();
error1.hide();
fromAddOrEdit.submit();
}
});
}
};
}();