jquery only allow input float number

Viewed 94097

i'm making some input mask that allows only float number. But current problem is I can't check if multiple dots entered. Can you check those dots and prevent it for me?

Live Code: http://jsfiddle.net/thisizmonster/VRa6n/

$('.number').keypress(function(event) {
    if (event.which != 46 && (event.which < 47 || event.which > 59))
    {
        event.preventDefault();
        if ((event.which == 46) && ($(this).indexOf('.') != -1)) {
            event.preventDefault();
        }
    }
});
14 Answers

Below Code I am allowing only Digits and Dot symbol.
ASCII characters number starts in 47 and ends with 58 and dot value is 190.

   $("#Experince").keyup(function (event) {
        debugger

        if ((event.which > 47
            && event.which < 58) ||event.which== 190) {
             if ($("#Experince").val().length > 3) {

        }
        } // prevent if not number/dot
        else {
             $("#Experince").val($("#Experince").val().slice(0, -1))
        }

    });

For simple cases and without hardcoding some html instructions would fit that pretty enough

<input type="number" step="0.01"/>
Related