Jquery apply key up function to all input boxes - Novice Question

Hi ,
I have the following bit of code, it isn't done inside document.ready or anything. it is literally this in side :<script></script> tags.
I have a table full of 300+ input boxes and whenever a non-numeric value is entered such as $ then it throws the error.

Unfortunately it works for the first box, but doesn't work thereafter..

Can anyone tell me what is wrong..

Cheers
John


jQuery(".input_price").keyup(function() {
    if(!Number(jQuery('.input_price').val())){
        jQuery('#J_msg').show();
        jQuery('#J_msg').html('You must only enter Numbers');
        jQuery(this).css('border','1px solid red');
        jQuery('#J_msg').css('background-color','red');
    }
    else{
        jQuery('#J_msg').html('Corrected');
        jQuery('#J_msg').css('background-color','#A0FF9F');
        jQuery(this).css('border','1px solid #7F9DB9');
        jQuery('#J_msg ').fadeOut(3000, function() {});
    }
});

Open in new window

LVL 2
jdav357Asked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
>>it is literally this in side :<script></script> tags.
it needs to be AFTER ALL the relevant input boxes OR execute that code upon document ready. Additionally, on the following line:
 if(!Number(jQuery('.input_price').val())){

change '.input_price' to "this" (but WITHOUT double quotes):
jQuery(function(){

jQuery(".input_price").keyup(function() {
    if(!Number(jQuery(this).val())){
        jQuery('#J_msg').show();
        jQuery('#J_msg').html('You must only enter Numbers');
        jQuery(this).css('border','1px solid red');
        jQuery('#J_msg').css('background-color','red');
    }
    else{
        jQuery('#J_msg').html('Corrected');
        jQuery('#J_msg').css('background-color','#A0FF9F');
        jQuery(this).css('border','1px solid #7F9DB9');
        jQuery('#J_msg ').fadeOut(3000, function() {});
    }
});

});

Open in new window

0
 
StealthyDevCommented:
what is the name/id of the textboxes?

you can use .each in jquey


0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.