jQuery - Change background of input field if text getting replaced

Posted by ywickham on Stack Overflow See other posts from Stack Overflow or by ywickham
Published on 2010-05-11T01:59:55Z Indexed on 2010/05/11 2:04 UTC
Read the original article Hit count: 236

I'm currently using jQuery to restrict a text box to number only input as follows:

    $('input.numbersOnly').keyup(function () { 
        this.value = this.value.replace(/[^0-9\.]/g,'');
    });

I'd like to let the user know that what they are typing is being rejected by changing the background color of the field. I know that I can change the background of a field using something like this:

     $('input.numbersOnly').addClass('alertClass');

My question is, how do I combine the two code examples above so that the color of the field changes as the character is getting replaced? My goal is to alert the user that something is wrong with their input as they type it in the field.

Thanks!

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about textbox