views:

1560

answers:

4

When my form doesn't validate, I want to disable the form's submit function. I can do this by disabling the submit button, but this would leave the enter key unchecked.
I can set the .submit(return false); but then I can't re-enable it again later.

Any suggestions?

A: 

Instead of directly calling the submit function call an intermediary function and do a check in there.

i.e.

var isSubmitable = false;

fucntion submitForm() {
   if(isSubmitable) {
       myform.submit();
   }
}
zodeus
+2  A: 

How about doing the check inside the submit method:

$('#form').submit (function() {
 if(formValidated())
  return true;
 return false;
});
Pim Jager
He, simple and working :)
borisCallens
Just a note you probably already knew about: Don't forget to also make your validations on the server-side though because having javascript disabled will bypass this.
lpfavreau
A: 

Another possible solution:

$('#form').change(function() { (isValid($('#form')) ? $('#submitBtn').attr('disabled',false) : $('#submitBtn').attr('disabled',true) });

Of course, isValid() checks that all your $('#form') elements comply to your validation rules. That's how I would do it.

karim79
+1  A: 

just a simplification of Pim's:


$('#form').submit (function() { return formValidated(); });
Nick Franceschina