如何在十进制数字后防止按键两位数? [英] How to prevent keypress two digits after a decimal number?

查看:112
本文介绍了如何在十进制数字后防止按键两位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项任务,要在十进制数字后防止按键两位数字。
我的jquery文件是

  $(function(){
$('#name')。 bind('paste',function(){
var self = this;
setTimeout(function(){
if(!/ ^ [a-zA-Z] + $ /。test ($(self).val()))
$(self).val('');
},0);
});

$('#salary')。bind('paste',function(){
var self = this;
setTimeout(function(){
if(!/ ^ \d * (\.\d {1,2})+ $ /。test($(self).val()))
$(self).val('');
}, 0);
});

$('。decimal')。keyup(function(){
var val = $(this).val();
if(isNaN(val)){
val = val.replace(/ [^ 0-9] ./ g,'');


if(val .split('。')。length> 2)
val = val.replace(/ \。+ $ /,);
}
$(this).val( val);
});
});

我的html页面是

 < b取代;姓名< / b个
< input type =textid =name/>< br />
< b>薪酬< / b>
< input type =textid =salaryclass =decimal/>

这里我只想在小数点后面写两位数字,我该怎么做?
您可以在 http://jsfiddle.net/V6s4B/ 中看到我的代码

解决方案

您可以在按键上的 keyup 之前处理关键事件,如果输入不符合我们的喜好,我们可以禁用事件发生。就像这样:



更新



不幸的是我的原始答案在某些无法表示的数字上失败准确地作为一个浮动。这是另一种解决方案,它通过一个方便的帮助函数检查'。字符的位置与字符串的长度。



jsFiddle

  $('。decimal')。keypress(function(e){
var character = String.fromCharCode(e.keyCode)
var newValue = this.value + character;
if(isNaN(newValue)|| hasDecimalPlace(newValue,3)){
e.preventDefault();
return false;
}
});

函数hasDecimalPlace(value,x){
var pointIndex = value.indexOf('。');
return pointIndex> = 0&& pointIndex< value.length - x;
}



原始答案



jsFiddle

  $('。decimal')。keypress(function(e){
var character = String.fromCharCode(e.keyCode)
var newValue = this.value + character;
if (isNaN(newValue)|| parseFloat(newValue)* 100%1> 0){
e.preventDefault();
return false;
}
});

注意 parseFloat(newValue)* 100%1>如果 newValue 包含的数字超过2位小数,则的计算结果为true。


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

$(function(){ 
    $('#name').bind('paste', function(){
    var self = this;
    setTimeout(function() {
        if(!/^[a-zA-Z]+$/.test($(self).val()))
            $(self).val('');
    }, 0);    
           }); 

        $('#salary').bind('paste', function(){
    var self = this;
    setTimeout(function() {
        if(!/^\d*(\.\d{1,2})+$/.test($(self).val()))
            $(self).val('');
    }, 0);    
           }); 

    $('.decimal').keyup(function(){
        var val = $(this).val();
        if(isNaN(val)){
             val = val.replace(/[^0-9]./g,'');


             if(val.split('.').length>2) 
                 val =val.replace(/\.+$/,"");
        }
        $(this).val(val); 
    });
    });      

My html page is

<b>Name</b>
<input type="text" id="name"  /><br/>
<b>Salary</b>
<input type="text" id="salary"  class="decimal" />

here i want only write 2 digits after decimal,how can i do this? You can see my code in http://jsfiddle.net/V6s4B/

解决方案

You can handle the key event before keyup on keypress, if the input is not to our liking we can disable the event from occurring. Something like this:

Update

Unfortunately my original answer below fails on certain numbers that can't be represented accurately as a float. Here is another solution that checks the position of the '.' character against the length of the string with a handy helper function.

jsFiddle

$('.decimal').keypress(function (e) {
    var character = String.fromCharCode(e.keyCode)
    var newValue = this.value + character;
    if (isNaN(newValue) || hasDecimalPlace(newValue, 3)) {
        e.preventDefault();
        return false;
    }
});

function hasDecimalPlace(value, x) {
    var pointIndex = value.indexOf('.');
    return  pointIndex >= 0 && pointIndex < value.length - x;
}

Original answer

jsFiddle

$('.decimal').keypress(function (e) {
    var character = String.fromCharCode(e.keyCode)
    var newValue = this.value + character;
    if (isNaN(newValue) || parseFloat(newValue) * 100 % 1 > 0) {
        e.preventDefault();
        return false;
    }
});

Note that parseFloat(newValue) * 100 % 1 > 0 evaluates to true if newValue contains a number that has more than 2 decimal places.

这篇关于如何在十进制数字后防止按键两位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆