Check if inputs are empty using jQuery

前端 未结 18 1159
灰色年华
灰色年华 2020-11-22 09:14

I have a form that I would like all fields to be filled in. If a field is clicked into and then not filled out, I would like to display a red background.

Here is my

18条回答
  •  余生分开走
    2020-11-22 09:37

    Everybody has the right idea, but I like to be a little more explicit and trim the values.

    $('#apply-form input').blur(function() {
         if(!$.trim(this.value).length) { // zero-length string AFTER a trim
                $(this).parents('p').addClass('warning');
         }
    });
    

    if you dont use .length , then an entry of '0' can get flagged as bad, and an entry of 5 spaces could get marked as ok without the $.trim . Best of Luck.

提交回复
热议问题