I am trying to create my own basic form validation without having to resort to heavy, one-size-fits-all plugins and I have written the following code. It doesn't seem to matter how often I rewrite it and start over, I can't seem to get it to work.
The idea is that the script checks the form to see if all the fields have been completed and if so it removes the disabled attribute from the submit button.
The Function:-
function checkForm(){
$('#contact :input').each(function(){
if($(this).attr('value') == null){
var checked = false;
} else {
var checked = true;
}
})
if (checked == true){
alert('all filled in');
//remove disabled attribute from button
} else {
alert('not completed');
//add disabled attribute to button
}
}
And the code that calls the function:-
$('#contact :input').blur(function(){
if ($(this).val() <= ''){
$(this).next('.error').show();
} else {
$(this).next('.error').hide();
checkForm();
}
})
I've been messing about with this all day and am struggling to find an answer via Google.
if ($(this).val() <= ''){