I am trying to create a simple application form with two buttons (save and submit). I am using the jQuery validate plugin. In order to submit the form the applicant must actually fill out the entire form correctly. No empty boxes, and valid data in each box. To save, however, they can leave as many fields blank as they would like, but the fields they fill out must be valid. (ie. no letters in boxes for numbers).
To this end, I have two separate validate functions and depending on which button the user clicks, one of the two is called. This works fine for the submit button, and it works the first time for the save button. However, if a user enters some gibberish into a box meant for numbers and hits save TWICE, the form will submit anyway. The first time they hit save, it behaves correctly.
For the life of me I cannot figure out why. My jquery code is below:
$(document).ready(function(){
$("#csa_submit_button").live("click",function(){
//alert("submit");
$("#csa_submit_flag").val("true");
validator = $("#csa_form").validate({
"rules": {
"csa_phone" : {required: true, digits: true, minlength: 10, maxlength: 10},
"csa_current_grants" : {number: true, required: true},
"csa_current_work_study" : {number: true, required: true},
"csa_balance_no_loans" : {number: true, required: true},
"csa_current_loans" : {number: true, required: true},
"csa_textbooks" : {number: true, required: true},
"csa_total_to_date" : {number: true, required: true},
"csa_parent_plus" : "required",
"csa_student_loans" : "required",
"csa_balance" : {number: true, required: true},
"csa_employment_contributions" : {number: true, required: true},
"csa_current_financial_situation" : "required",
"csa_activities" : "required",
"csa_request_amount" : {required: true, number: true},
"csa_term": {required: true, minlength: 1}
},
"messages": {
"csa_term": "You must select at least one term",
"csa_phone": "You must enter a phone number in the following format: ##########"
},
errorPlacement: function(error, element) {
if (element.attr('type') === 'radio') {
error.insertAfter(element.parent());
}
else if (element.attr('name') === 'csa_employment_contributions'){
error.insertAfter(element.parent());
}
else if (element.attr('name') === 'csa_term') {
error.insertAfter(element.parent());
}
else {
error.insertAfter(element);
}
}
});
});
$("#csa_save_button").live("click",function(){
$("#csa_submit_flag").val("false");
validator = $("#csa_form").validate({
"rules": {
"csa_phone" : {number: true},
"csa_current_grants" : {number: true},
"csa_current_work_study" : {number: true},
"csa_balance_no_loans" : {number: true},
"csa_current_loans" : {number: true},
"csa_textbooks" : {number: true},
"csa_total_to_date" : {number: true},
"csa_balance" : {number: true},
"csa_employment_contributions" : {number: true},
"csa_request_amount" : {number: true}
},
"messages": {
"csa_phone": "You must enter a phone number in the following format: ##########"
},
errorPlacement: function(error, element) {
if (element.attr('name') === 'csa_employment_contributions'){
error.insertAfter(element.parent());
} else {
error.insertAfter(element);
}
}
});
});
});