Check if input empty on submit

后端 未结 9 686
谎友^
谎友^ 2020-12-25 08:36

I\'ve been looking around for a solution to this, but can\'t seem to find any examples that work for me. Here\'s what I\'ve got so far:

$(\"#register-form\")         


        
相关标签:
9条回答
  • 2020-12-25 09:21

    Simple Solution !:

     function checkForm(){
            $("input.required").css("border","1px solid #AFAFAF");
            $("input.required[value=]").css("border-color","red");
            if($("input.required").val().length<2)return false;
            return true;
        }
    
    0 讨论(0)
  • 2020-12-25 09:23

    Basing on David Fell's answer, (that has an error, in my opinion) you could do this:

    $("#register-form").submit(function() {
      $('.required input').each(function() {
        if ($(this).val() == '') {           
          $(this).addClass('highlight');
        }
      });
    
      if ($('.required input.highlight').size() > 0) {
        alert("Please fill in all the required fields (indicated by *)");
        return false;
      }
    } 
    
    0 讨论(0)
  • 2020-12-25 09:27

    You can't reference the values of all the form inputs like that.

    var valid = true;
    $('.required input').each(function(){
       if( $(this).val() == '' ){
          valid = false;
          $(this).addClass('highlight');
       }
    });
    

    The forms plugin will do this for you, by the way.

    0 讨论(0)
提交回复
热议问题