Check if input field is empty on fields with jQuery

html, javascript, jquery

Solution

Try:

$('.submit').click(function(e){
 if(!$('.required').val()) {
    $('.required').addClass('error');
    e.preventDefault();
  } else {
    return true;
  };
});

Problem

I have a form with 5 fields all with the class 'required' Im trying to ensure that on submit these fields arent empty, if they are, add a class, if not, return true - ive tried the following only with no luck, even if the fields are empty the form still submits. ``` $('.submit').click(function(){ if($('.required').val() == "") { $('.required').addClass('error'); return false; } else { return true; }; }); ```

Original source