views:

70

answers:

4

What I want to do is take an input for a zipcode and in jQuery if input#zip has 5 characters then function. Also same for a list box when the user chooses one of the choices, might be simpler?

A: 

Perhaps use the .change(). Each time change is fired get the length of the input and, if its 5 or more, run your code.

SquidScareMe
+3  A: 
$('#zip-input').keyup(function(){ 
    if($(this).val().length == 5) {
    //do your stuff here
    }
})
Moin Zaman
Thanks, this is perfect!
sway
+2  A: 

For your zip scenario:

$("#zip").keypress(function() {
  if ($(this).val() && $(this).val().length == 5) {
    someFunction($(this).val());
  }
});

For your listbox scenario:

$("#listbox").change(function() {
  if ($(this).val()) {
    someFunction($(this).val());
  }
});
villecoder
Thanks for your solutions, works great!
sway
+2  A: 

You might want to use a keyup event handler for instance.

$('input').bind('keyup', function(){
   if($(this).val().length >= 5){
      alert('5 characters');
      return false;
   }
});
jAndy