Allow only numbers and one dot in input

后端 未结 1 1334
醉酒成梦
醉酒成梦 2021-01-07 00:46

Using the following code with jQuery:

$(\'#from-amount\').keypress(function(event) {
    if ((event.which != 46 || $(this).val().indexOf(\'.\') != -1) &&         


        
相关标签:
1条回答
  • 2021-01-07 01:07

    Try this

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

    https://jsfiddle.net/4rsv960t/1/

    0 讨论(0)
提交回复
热议问题