在文本框的按键中限制为2位小数? [英] Restrict to 2 decimal places in keypress of a text box?

查看:133
本文介绍了在文本框的按键中限制为2位小数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本框中输入小数点。我想通过在小数点后输入2位以上来限制用户。我已经在Keypress事件中编写了实现该代码的代码。

I want to enter a decimal point in a text box. I want to restrict the user by entering more than 2 digits after the decimal point. I have written the code for achieving that in the Keypress event.

function validateFloatKeyPress(el, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;

    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }

    if (charCode == 46 && el.value.indexOf(".") !== -1) {
        return false;
    }

    if (el.value.indexOf(".") !== -1)
    {
        var range = document.selection.createRange();

        if (range.text != ""){
        }
        else
        {
            var number = el.value.split('.');
            if (number.length == 2 && number[1].length > 1)
                return false;
        }
    }

    return true;
}
<asp:TextBox ID="txtTeamSizeCount" runat="server" onkeypress="return validateFloatKeyPress(this,event);" Width="100px" MaxLength="6"></asp:TextBox>

代码正在运作,但问题是:如果我输入。75然后将其更改为1.75,这是不可能的。唯一的方法是完全删除它,然后键入1.75。如果文本框中的小数点后面已有2位数,则会出现此问题。我施加的条件是

The code is working but the issue is: if I enter ".75" and then change it to "1.75", it is not possible. Only way to do it is delete it completely and then type "1.75". This issue occurs if there are already 2 digits after decimal in the textbox. The conditions that I impose are

a)存在小数后,它必须至少有1或2位数。对于ex .75或.7或10.75或333.55或333.2 被接受。但不是 .753或12.3335

a) After decimal is present, it must at least have 1 or 2 digits. For ex .75 or .7 or 10.75 or 333.55 or 333.2 is accepted. but not .753 or 12.3335


b)在小数之前,用户输入值不是必须的。用户还必须能够输入整数。


b) Before the decimal, it not a must for the user to enter a value. User must also be able to enter integer numbers also.

你能告诉我可能是什么问题吗?

Can you tell me what could be the issue?

谢谢,

Jollyguy

Thanks,
Jollyguy

推荐答案

你几乎就在那里。只需检查小数点后不超过2个字符。

You were almost there. Just check that there are no more than 2 characters after the decimal.

更新1 - 检查克拉位置以允许在小数点前插入字符。

更新2 - 正确的问题由ddlab的评论指出,只允许一个点。

UPDATE 1 - check carat position to allow character insertion before the decimal.
UPDATE 2 - correct issue pointed out by ddlab's comment and only allow one dot.

 function validateFloatKeyPress(el, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    var number = el.value.split('.');
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }
    //just one dot (thanks ddlab)
    if(number.length>1 && charCode == 46){
         return false;
    }
    //get the carat position
    var caratPos = getSelectionStart(el);
    var dotPos = el.value.indexOf(".");
    if( caratPos > dotPos && dotPos>-1 && (number[1].length > 1)){
        return false;
    }
    return true;
}

//thanks: http://javascript.nwbox.com/cursor_position/
function getSelectionStart(o) {
    if (o.createTextRange) {
        var r = document.selection.createRange().duplicate()
        r.moveEnd('character', o.value.length)
        if (r.text == '') return o.value.length
        return o.value.lastIndexOf(r.text)
    } else return o.selectionStart
}

http://jsfiddle.net/S9G8C/1/

http://jsfiddle.net/S9G8C/203/

这篇关于在文本框的按键中限制为2位小数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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