开发者

Decimal point appearing in a textbox, should not be allowed to delete (in Jquery)

I have a textbox containing value 1234.567, the user is allowed to edit the digits, but the decimal point shoul开发者_Python百科d not be removed in any case.How do i achieve this in Jquery??? (I want to avoid using plug-ins for this)


There may be a bit of a nicer way to do this, but this is what sprung to mind first off. Something along these lines should work. It simply remembers each previous valid value of the input, and if it detects an invalid value (i.e. one with no . character) it returns the value of the input to what it was previously:

var current = $("#example").val();
$("#example").keyup(function() {
    if($(this).val().indexOf(".") === -1) {
        $(this).val(current);  
    } 
    else {
        current = $(this).val();   
    }
});

Here's a working example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜