I am using the jQuery validation plugin and have a few forms that require different rules but instead of writing out the same code again like below
if (jQuery().validate) {
var removeSuccessClass = function(e) {
$(e).closest('.form-group').removeClass('has-success');
}
$('#validation-form').validate({
errorElement: 'span', //default input error message container
errorClass: 'help-block', // default input error message class
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
focusInvalid: false, // do not focus the last invalid input
ignore: ":hidden:not(.chosen)",
invalidHandler: function (event, validator) { //display error alert on form submit
},
highlight: function (element) { // hightlight error inputs
$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change dony by hightlight
$(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
setTimeout(function(){removeSuccessClass(element);}, 3000);
},
submitHandler: function(form){
$('#save').hide();
$('#saving').show();
form.submit();
}
});
$('#validation-form-pdf-word').validate({
rules: {
file: {
extension: "pdf|doc|docx"
}
},
errorElement: 'span', //default input error message container
errorClass: 'help-block', // default input error message class
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
focusInvalid: false, // do not focus the last invalid input
ignore: ":hidden",
invalidHandler: function (event, validator) { //display error alert on form submit
},
highlight: function (element) { // hightlight error inputs
$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change dony by hightlight
$(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
setTimeout(function(){removeSuccessClass(element);}, 3000);
},
submitHandler: function(){
$('#save').hide();
$('#saving').show();
form.submit();
}
});
$('#validation-form-pdf').validate({
rules: {
file: {
required: true,
extension: "pdf"
}
},
errorElement: 'span', //default input error message container
errorClass: 'help-block', // default input error message class
errorPlacement: function(error, element) {
if(element.parent('.input-group').length) {
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
},
focusInvalid: false, // do not focus the last invalid input
ignore: ":hidden",
invalidHandler: function (event, validator) { //display error alert on form submit
},
highlight: function (element) { // hightlight error inputs
$(element).closest('.form-group').removeClass('has-success').addClass('has-error'); // set error class to the control group
},
unhighlight: function (element) { // revert the change dony by hightlight
$(element).closest('.form-group').removeClass('has-error'); // set error class to the control group
setTimeout(function(){removeSuccessClass(element);}, 3000);
},
submitHandler: function(){
$('#save').hide();
$('#saving').show();
form.submit();
}
});
}
I would like to have it so i can just pass the rest of the rules through something like this
if (jQuery().validate) {
var removeSuccessClass = function(e) {
$(e).closest('.form-group').removeClass('has-success');
}
$('#validation-form').validate({
theRules
});
$('#validation-form-pdf-word').validate({
rules: {
file: {
extension: "pdf|doc|docx"
}
},
theRules
});
}