在@ Html.Textbox中强制输入为十进制 [英] Force input to be decimal in @Html.Textbox

查看:135
本文介绍了在@ Html.Textbox中强制输入为十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  @ Html.TextBox(imp,金额,新{@class = alignRight,size = 5})

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

解决方案

是你可以通过Jquery和Javascript做到

  function isNumeric(evt)
{
var c = (evt.which)? evt.which:event.keyCode
if((c> ='0'&& c< ='9')|| c =='。')
return true;
返回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 = ;
}
}


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?

解决方案

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 = "";
    }
}

这篇关于在@ Html.Textbox中强制输入为十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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