开发者

How to test input value for null or zero with jQuery

开发者 https://www.devze.com 2022-12-28 22:48 出处:网络
I need to keep the focus on the text input when the user moves to the next input without put开发者_如何学JAVAting something in the previous one. In short, if it is null (left blank) or not valid, how

I need to keep the focus on the text input when the user moves to the next input without put开发者_如何学JAVAting something in the previous one. In short, if it is null (left blank) or not valid, how do I keep the focus on that input until the conditions are satisfied?


$("#inputID").blur(function(){
            if ("#inputID").val() == "")
            {
                  $("#inputID").focus();
            }
    });

Code done this way for the concept of explaining that is always referring to the same input. In practice 'this' would be a better option.


$( element ).blur( function( e ) {
   var ev = e || event;
   if( $( this ).val() == '' ) {
      $( this ).focus(); 
      return false;
   }
});


You don't really need jquery for it <input type="text" onblur="if (!this.value) this.focus()">

Although, I don't think that it's a good idea. If it's a form, then it's better to show the list of required fields when the user tries to submit it.

0

精彩评论

暂无评论...
验证码 换一张
取 消