How to check if any of my text box is empty or not in javascript

Possible duplicate:
Check for empty entries using jQuery

I have a form and text fields, how can I determine if any of these text fields is empty using javascript if else after clicking the form button.

function checking() {
    var textBox = $('input:text').value;
    if (textBox == "") {
        $("#error").show('slow');
    }
}

Thanks in advance!

+5
source share
3 answers

Using jQuery selectors to select elements, you have a jQuery object, and you must use the method val()to get / set the value of the input elements.

Also note that the selector is :textdeprecated, and it would be better to trim the text to remove whitespace. You can use the utility function $.trim.

function checking() {
    var textBox =  $.trim( $('input[type=text]').val() )
    if (textBox == "") {
        $("#error").show('slow');
    }
}

value, jQuery DOM. [index] get.

 var textBox = $('input[type=text]')[0].value;

, .

function checking() {
    var empty = 0;
    $('input[type=text]').each(function(){
       if (this.value == "") {
           empty++;
           $("#error").show('slow');
       } 
    })
   alert(empty + ' empty input(s)')
}
+12

value jquery val(), , .

Live Demo

function checking() {    
  var textBox = $('input:text').val();
  if (textBox == "") {
      $("#error").show('slow');
   }
}

blur losing focus from each textbox.

Live Demo

$('input:text').blur(function() {    
    var textBox = $('input:text').val();
    if (textBox == "") {
        $("#error").show('slow');
    }
});

submit button OP

Live Demo

$('#btnSubmit').click(function() { 
     $("#error").hide();    
     $('input:text').each(function(){
       if( $(this).val().length == 0)
          $("#error").show('slow');
    });
});
+5

Try this code:

var textBox = $('input:text').val();

if (textBox==""){ 
  $("#error").show('slow'); 
} 
+1
source

All Articles