Posted By

keidash on 05/27/13


Tagged

number jquery keypress keycode


Versions (?)

Only Numbers in Web Controls with jQuery


 / Published in: jQuery
 

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

  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  

You need to login to post a comment.