views:

3744

answers:

6

How do I identify empty textboxes using jQuery? I would like to do it using selectors if it is at all possible. Also, I must select on id since in the real code where I want to use this I don't want to select all text inputs.

In my following two code examples the first one accurately displays the value typed into the textbox "txt2" by the user. The second example identifies that there is an empty textbox, but if you fill it in it still regards it as empty. Why is this?

Can this be done using just selectors?

This code reports the value in textbox "txt2":

<html>
<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript">
  $(function() {
   $('#cmdSubmit').click(function() {
    alert($('[id=txt2]').val());
   });    
  });
 </script>
</head>
<body>
 <form>
  <input type="text" name="txt1" id="txt1" value="123" /><br />
  <input type="text" name="txt2" id="txt2" value="" /><br />
  <input type="text" name="txt3" id="txt3" value="abc" /><br />
  <input type="submit" name="cmdSubmit" id='cmdSubmit' value="Send" /><br />
 </form>
</body>
</html>

This code always reports textbox "txt2" as empty:

<html>
<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
 <script type="text/javascript">
  $(function() {
   $('#cmdSubmit').click(function() {
    if($('[id^=txt][value=""]').length > 0) {
     if (!confirm("Are you sure you want to submit empty fields?")) {
      if (event.preventDefault) {
       event.preventDefault();
      }
      else {
       event.returnValue = false;
      }
     }
    }
   });    
  });
 </script>
</head>
<body>
 <form>
  <input type="text" name="txt1" id="txt1" value="123" /><br />
  <input type="text" name="txt2" id="txt2" value="" /><br />
  <input type="text" name="txt3" id="txt3" value="abc" /><br />
  <input type="submit" name="cmdSubmit" id='cmdSubmit' value="Send" /><br />
 </form>
</body>
</html>
+4  A: 
$(":text[value='']").doStuff();

?

By the way, your call of:

$('input[id=cmdSubmit]')...

can be greatly simplified and speeded up with:

$('#cmdSubmit')...
cletus
I guess there's a closing square bracket missing. A common source of jQuery not working as expected, hard to spot :-/
OregonGhost
Your first example is basically what my not working example does. I want to know if the user added text to the input field, your example does not do this.
Cros
I was hasty, your example seems to work, but it doesn't select on id, which I must do.
Cros
+8  A: 

Another way

$('input:text').filter(function() { return $(this).val() == ""; });

or

$('input:text').filter(function() { return this.value == ""; });

or

$('input:text[value=""]');

Working Demo

code from the demo

jQuery

 $(function() {

  $('#button').click(function() {

    var emptyTextBoxes = $('input:text').filter(function() { return this.value == ""; });
    var string = "The blank textbox ids are - \n";

    emptyTextBoxes.each(function() {
      string += "\n" + this.id;
    });
    alert(string);
  });

});
Russ Cam
For me:if($('[id^=txt]').filter(function() { return $(this).val() == ""; }).length > 0) { alert('WARNING');}
Cros
What separates your last example from my second one? I need to select on id, since in the real code I won't look at all text inputs.My second example doesn't work with user input, but yours does.
Cros
Why does $('input:text[value=""]'); work, when $('[id^=txt][value=""]'); does not?
Cros
The working selector seems to be to do with :text in the selector - if you open the working demo and add /edit to the URL, you can play with the selector used. If you take off the :text, the selector no longer works correctly, even with $('input[value=""]'). Possibly a bug in the Sizzle selector engine, but I don't have time to investigate. By the way, I would suggest using an element tag in the selector, otherwise each element will be checked to see if there is a match for the attribute values.
Russ Cam
Thanks! It's as you say, and I have added my own answer with that information. Good tip about element tags as well.
Cros
Why are you guys making life so complicated? There is no need for this much code.
Antony Carthy
@Antony- show a better way and I'll upvote it
Russ Cam
+3  A: 

You could also do it by defining your own selector:

$.extend($.expr[':'],{
    textboxEmpty: function(el){
        return $(el).val() == "";
    }
});

And then access them like this:

alert($(':text:textboxEmpty').length); //alerts the number of text boxes in your selection
James Wiseman
A: 

I'd recommend:

$('input:text:not([value])')
TM
This won't work, because [attribute] looks for the presence of an attribute - not whether it has a value or not. `<input value="">` will still match.
Beejamin
A: 

This will select empty text inputs with an id that starts with "txt":

$(':text[value=""][id^=txt]')
Cros
A: 

Am I right that the :empty filter is made for this job?

$('input[type=text]:empty').doStuff();
Antony Carthy
read the docs - http://docs.jquery.com/Selectors/empty. Empty is for elements that have no children, NOT no value
Russ Cam