I'm creating a register form with jQuery, and I'm trying to add a function to be run upon completion with no errors.开发者_运维问答 What I'm trying to figure out is how to get it run after all the rest is done, so I know what my variable 'hasErrors' is, rather than undefined. Here is my relevant code:
$('form.register .submit').click(validateRegister);
function validateRegister(){
var hasError = false;
//This is executed if something goes wrong. Basically I want my success() function to be run if this is never run, so at the end.
function returnErrors() {
$processing.hide();
$this.parent().removeClass('processing');
$endNote.slideUp();
hasError = true;
}
if (hasErrors == false) {
success();
}
return false;
}
Any help with this is extremely appreciated. Thank you!
I think you want to do something like:
function validate() {
var isValid = false;
// do some validation here
// if it passes, set isValid = true
if (isValid) {
success();
return true;
} else {
error();
return false;
}
}
jQuery also has an add-on for form validation that you might be interested in, but it could be overkill depending on how complex your needs are.
http://docs.jquery.com/Plugins/validation
精彩评论