【问题标题】:Force input to be decimal in @Html.Textbox在@Html.Textbox 中强制输入为十进制
【发布时间】:2013-05-20 09:16:05
【问题描述】:

我有问题。 我有一个 Razor 文本框:

 @Html.TextBox("imp",   amount, new { @class = "alignRight", size = 5 })

我希望,在输入中,用户将只写入十进制值。 我能怎么做? 我认为 jquery 可以帮助我......但是......那个文本框的 id 是什么?

【问题讨论】:

  • 文本框的 id 将是“imp”。

标签: c# jquery html asp.net-mvc-4 razor


【解决方案1】:

是的,您也可以通过 Jquery 和 Javascript 进行操作

function isNumeric(evt)
{
 var c = (evt.which) ? evt.which : event.keyCode
 if ((c >= '0'  && c <= '9') || c == '.' )
    return true;
 return false;
}

关于Id,你也可以通过类来完成。 在你的情况下,它会是

@class = "alignRight"

同样来自http://www.webdeveloper.com/forum/showthread.php?124756-Stop-pasting-of-invalid-text-into-decimal-only-textbox

function validateDecimal(textboxIn)
{
    var text = textboxIn.value

    for(var x = 0; x < 10; x++)
    {
        while(text.indexOf(x) != -1)
        {
            text = text.replace(x,"");
        }
    }

    if(text != "." && text != "")
    {
        textboxIn.value = "";
    }
}

【讨论】:

    猜你喜欢
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    相关资源
    最近更新 更多