Isn’t that only if you’re using the W3C model of event registration? Otherwise couldn’t you just write return false; at the end of the submit() function…
Okay but if i write return false at the end, the whole submit will not take place as per my scenario, i want it to be canceled only when, let’s say i found that there is an empty field, otherwise it should continue normally.
— never mind i can check for a Boolean empty or not…
Isn’t that only if you’re using the W3C model of event registration? Otherwise couldn’t you just write return false; at the end of the submit() function…
Usually, I like suggesting use of unobtrusive JavaScript to people (as long as no one has to jump through hoops).
You can use return false, but you would have to use the return keyword in the attribute on function call.
function f()
{
//validate
//return (true or false)
}
HTML FORM
onsubmit='return f()'
Okay but if i write return false at the end, the whole submit will not take place as per my scenario, i want it to be canceled only when, let’s say i found that there is an empty field, otherwise it should continue normally.
You can do it with conditionals:
If you like too, you can use the filter method instead of each, and return true from each filtered item if they fail validation. That way you can check the size of the end result, and if there are any elements remaining you know the validation failed.
The following is some pseudocode that indicated the type of things you can do with the filter method, instead of the each method.
var $invalidFields = $(selector).filter(function () {
var notValid = false;
if (condition) {
// do stuff
} else {
// do failed stuff
notValid = true;
return (notValid);
});
if ($invalidFields.size() > 0) {
// failed validation
return false;
}