How to prevent keypress two digits after a decimal number?

后端 未结 5 2011
花落未央
花落未央 2020-12-31 10:06

I have got a task to prevent keypress two digits after a decimal number. My jquery file is

$(function(){ 
    $(\'#name\').bind(\'paste\', function(){
             


        
5条回答
  •  佛祖请我去吃肉
    2020-12-31 10:21

    $("#salary").keyup(function(){
        var number = ($(this).val().split('.'));
        if (number[1].length > 2)
        {
            var salary = parseFloat($("#salary").val());
            $("#salary").val( salary.toFixed(2));
        }
      });
    

    http://jsfiddle.net/calder12/fSQpc/

    Stop letters from going in the box, you'll have to put the two together I haven't time.

        if (this.value.match(/[^0-9]./g)) {
          this.value = this.value.replace(/[^0-9]./g, '');
          return false;
        }
    

提交回复
热议问题