Skip to content Skip to sidebar Skip to footer

Checking If Alteast One Of The Text Box Is Filled - Jquery

From the HTML and JQuery below, I am trying to check if at least one the text box has value. It is working just fine. I would like to know if there is any better and shorter approa

Solution 1:

You can achieve this in better way:

var textboxes = $('[id^=text]');
var emptytextboxes = textboxes.filter(function(){
  returnthis.value == "";
});

if(textboxes.length == emptytextboxes.length){
   alert('atleast one of the field should be filled');  
}  

Working Demo

Solution 2:

Just add the "textboxes" class to each input types, and iterate through them with this tested and working snippet.

functionchecker(){
var bool = false;


$('.textboxes').each(function() {
  if (!$(this).val() == '') {
       bool = true;
  }
   });
    alert(bool)
}

Link for the demo.

Solution 3:

Try this,though havent tested it,

functionchecker(){
    var x="";
    $("input[type='text']").each(function(){
        if($(this).val()=="")
            x=x+"y";
    });

    if(x.length>0)            
    {            
         alert('atleast one of the field should be filled');   
    }
}

Solution 4:

This would also work:

var isEmpty= false;
$("input[type=text]").each(function() {
      if($(this).val().length > 0)
         {
            isEmpty = true;
            returnfalse; //break out of the loop
         }
});  


if(!isEmpty ){
   alert('atleast one of the field should be filled');  
} 

Here is a DEMO

Post a Comment for "Checking If Alteast One Of The Text Box Is Filled - Jquery"