-2

I'm using ASP.NET with jQuery. I have a TextBox and I will insert only float values, i.e: 11.33. I'll control the inserted characters from keyboard when writing the value 11.33. If the user enters a character which is not a number or a '.', then the TextBox should ignore it and not insert it.

How can I do?

Jesse
  • 8,605
  • 7
  • 47
  • 57
Selçuklu Ebrar
  • 2,059
  • 3
  • 14
  • 11

1 Answers1

1

use this jquery method

$('.number').keypress(function(event) {
  if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
    event.preventDefault();
  }
});

Edit: It is always also better to double check back at the server side, which means adding validation code at your code behind also

Hassan Mokdad
  • 5,832
  • 18
  • 55
  • 90