Only Numbers in Web Controls with jQuery


/ Published in: jQuery
Save to your folder(s)

Example to manipulate only numbers (and tab key) in a web control (textbox example)


Copy this code and paste it in your HTML
  1. $('[id$=txtControl]').bind('keypress', function (e) {
  2. onlyNumbers(e);
  3. });
  4.  
  5. function onlyNumbers(e) {
  6. var result = true;
  7. var items = [9, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
  8. var code = (e.keyCode ? e.keyCode : e.which);
  9. $.each(items, function () {
  10. if (code == this) {
  11. result = false;
  12. }
  13. });
  14.  
  15. if (result == true) {
  16. e.preventDefault();
  17. }
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.