I am trying to validate a form using jQuery and what I want to do now is to disable the submit button until all fields are filled correctly. This is my approach: http://jsfiddle.net/w57hq430/
<input type="text" name="commodity">Commodity
<input type="text" name="plz">PLz
<input type="submit">
and the actual jQuery:
$(document).ready(function() {
var error = null;
$('input[name=commodity]').focusout(function() {
var com = $('input[name=commodity]').val();
if($.isNumeric(com)) {
error ="asd";
}
else {
alert("commodity has to be numeric");
}
});
$('input[name=plz]').focusout(function() {
var com = $('input[name=plz]').val();
if($.isNumeric(com)) {
error ="asd";
}
else {
alert("plz has to be numeric");
}
});
if(error != null) {
$('input[type=submit]').attr('disabled', 'disabled');
}
else {
$('input[type=submit]').removeAttr('disabled');
}
});
I know that this code would prevent the submit button from being clicked when all fields are correct as this was meant to be a test if I use mouseout correctly.However,this is not working.The variable error is null even after you enter some numerics into the textfields(which should set it to "asd").Is my variable not accessible by the rest of the code?Or is anything else wrong?