Check if inputs are empty using jQuery

前端 未结 18 1307
灰色年华
灰色年华 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条回答
  •  猫巷女王i
    2020-11-22 09:45

    Here is an example using keyup for the selected input. It uses a trim as well to make sure that a sequence of just white space characters doesn't trigger a truthy response. This is an example that can be used to begin a search box or something related to that type of functionality.

    YourObjNameSpace.yourJqueryInputElement.keyup(function (e){
       if($.trim($(this).val())){
           // trimmed value is truthy meaning real characters are entered
        }else{
           // trimmed value is falsey meaning empty input excluding just whitespace characters
        }
    }
    

提交回复
热议问题