Joel
Joel

Reputation: 327

Replacing Characters other than numbers in textbox

I want to allow only numbers(1 to 5), a $(dollar) .(decimal) and '(singlequote) in a textbox. I want to do this on keyup event. If user types anything other than these character, it should be replaced with a *. Please tell how can I do it?

Upvotes: 1

Views: 924

Answers (1)

coreyward
coreyward

Reputation: 80051

You can strip the string on key-up like this:

$('input[type=text]').keyup(function(){
  $(this).val($(this).val().replace(/[^1-5$.']/g, '*'));
});

Keep in mind that you should verify this on the server side (there are a plethora of ways around it on the client side).

Upvotes: 3

Related Questions