jQuery - numeric, number only function

0 votes
707 views
added Sep 20, 2018 in jQuery by LC Marshal Captain (25,790 points)
jQuery.fn.ForceNumericOnly =
  function() {
      return this.each(function() {
          $(this).keydown(function(e)
          {
              var key = e.charCode || e.keyCode || 0;
              // allow backspace, tab, delete, enter, arrows, numbers and keypad numbers ONLY
              // home, end, period, and numpad decimal
              return (
                  key == 8 || 
                  key == 9 ||
                  key == 13 ||
                  key == 46 ||
                  key == 110 ||
                  key == 190 ||
                  (key >= 35 && key <= 40) ||
                  (key >= 48 && key <= 57) ||
                  (key >= 96 && key <= 105));
          });
      });
  };
var numericField = $('#edit-profile-basic-field-phone-number-und-0-value, #edit-profile-basic-field-zip-und-0-value, #edit-profile-basic-field-account-number-und-0-value, edit-profile-basic-field-ic-und-0-value ');

$(numericField).ForceNumericOnly();

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...