jquery only allow input float number jquery only allow input float number jquery jquery

jquery only allow input float number


You can check for the period in the same statement.

Also, you need to use the val method to get the value of the element.

Also, you want to check for the interval 48 to 57, not 47 to 59, otherwise you will also allow /, : and ;.

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


I think you guys have missed the left right arrows, delete and backspace keys.

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


I think everybody forgot the case of pasting text with the mouse, in which you can't detect the keystrokes, because there's none. Here's another approach I have been working on.

// only integer or float numbers (with precision limit)// example element: <input type="text" value="" class="number" name="number" id="number" placeholder="enter number" />$('.number').on('keydown keypress keyup paste input', function () {    // allows 123. or .123 which are fine for entering on a MySQL decimal() or float() field    // if more than one dot is detected then erase (or slice) the string till we detect just one dot    // this is likely the case of a paste with the right click mouse button and then a paste (probably others too), the other situations are handled with keydown, keypress, keyup, etc    while ( ($(this).val().split(".").length - 1) > 1 ) {        $(this).val($(this).val().slice(0, -1));        if ( ($(this).val().split(".").length - 1) > 1 ) {            continue;        } else {            return false;        }    }    // replace any character that's not a digit or a dot    $(this).val($(this).val().replace(/[^0-9.]/g, ''));    // now cut the string with the allowed number for the integer and float parts    // integer part controlled with the int_num_allow variable    // float (or decimal) part controlled with the float_num_allow variable    var int_num_allow = 3;    var float_num_allow = 1;    var iof = $(this).val().indexOf(".");    if ( iof != -1 ) {        // this case is a mouse paste (probably also other events) with more numbers before the dot than is allowed        // the number can't be "sanitized" because we can't "cut" the integer part, so we just empty the element and optionally change the placeholder attribute to something meaningful        if ( $(this).val().substring(0, iof).length > int_num_allow ) {            $(this).val('');            // you can remove the placeholder modification if you like            $(this).attr('placeholder', 'invalid number');        }        // cut the decimal part        $(this).val($(this).val().substring(0, iof + float_num_allow + 1));    } else {        $(this).val($(this).val().substring(0, int_num_allow));    }    return true;});