Tuesday, 8 November 2011

JQuery extension: numeric only input text

Here is the simply but re-usable approach, IMO: create an jquery numeric extension and then use the numeric extension in your input text box:

 
// numeric extension
jQuery.fn.onlyNumeric = function () {
    return this.each(function () {
        $(this).keydown(function (e) {
            var key = e.which || e.keyCode;
            if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
                key >= 48 && key <= 57 || key >= 96 && key <= 105 ||
                key == 8 || key == 13 || key == 46 ||
                key == 35 || key == 36 || key == 37 || key == 39)
                return true;
            return false;
        });
    });
};
// use it in your input text box
$('#input_box').onlyNumeric();
 

1 comment:

  1. An ios course introduces Apple application development fundamentals step by step. It simplifies Swift syntax and interface design concepts. This ios course focuses on practical implementation. It improves logical thinking and coding accuracy. Learners gain hands-on experience. It is dependable.

    ReplyDelete