0

I have following jQuery function to make textbox accepting only decimal number, but problem is it didn't accept . , so it works as only integer textbox,

I don't want to use any plugin

here is the jQuery function :

$(document).ready(function () {
    $("#textbox1").keydown(function(event)
    {
        if (event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 ||
        (event.keyCode == 65 && event.ctrlKey === true))
        {
            return;
        }
        else if(event.keyCode == 46)
        {
            return;
        }
        else 
        {
            if ((event.keyCode < 48 || event.keyCode > 57) && 
                (event.keyCode < 96 || event.keyCode > 105 ))
            {
                event.preventDefault(); 
            }   
        }        
    });
});​

Fiddle is here

0

2 Answers 2

3

and what about 4.5.2 ?

your code is not checking it !

why not using regex ?

[0-9]+(\.[0-9][0-9]?)?

just check the match when blur event

Sign up to request clarification or add additional context in comments.

Comments

2
$("#textbox1").keydown(function(e){
    var key = e.which;

    // backspace, tab, left arrow, up arrow, right arrow, down arrow, delete, numpad decimal pt, period, enter
    if (key != 8 && key != 9 && key != 37 && key != 38 && key != 39 && key != 40 && key != 46 && key != 110 && key != 190 && key != 13){
        if (key < 48){
            e.preventDefault();
        }
        else if (key > 57 && key < 96){
            e.preventDefault();
        }
        else if (key > 105) {
            e.preventDefault();
        }
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.