Force input to be decimal in @Html.Textbox

2019-08-03 15:32发布

i have a problem. I have a Razor Textbox:

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

I would like that , in input, user will write only decimal values. How can i do? I think jquery could help me...but...what's the id of that textbox?

1条回答
我只想做你的唯一
2楼-- · 2019-08-03 16:03

Yes you can do via Jquery and Javascript as well

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

And regarding Id, you can do it via class as well. In your case it will be

@class = "alignRight"

Also from 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 = "";
    }
}
查看更多
登录 后发表回答