I am using a form wizard in django and I want to allow the form to submit only if a Javascript condition is true.
For starters, let’s say that I always want to block the form submission. So, I am trying something like this:
$('form[name=mainform]').submit(function(event) {
alert('Submit blocked!');
event.preventDefault();
return false;
});
However, the form is always submitted normally and moves to the next step of the wizard !
I tried everything (setting the onClick of the submit button and return false) but nothing worked. I have used similar techniques and they were working in normal forms (not django). Can anybody explain to me what’s the problem ?
I have to mention that for my own reason I don’t want to do it through Django (by inserting a hidden field and setting its value through javascript and then checking it during the clean form).
Thanks !
Add the listener to the submit button of the form, not the form itself.