如何锁定键盘并仅允许将数字输入Mvc5中的Editorfor / Textbox? [英] How Do I Lock The Keyboard And Only Allow Numbers To Be Input Into An Editorfor/Textbox In Mvc5?

查看:165
本文介绍了如何锁定键盘并仅允许将数字输入Mvc5中的Editorfor / Textbox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对锁定键盘的代码有问题,只允许输入数值

i一直在视图中尝试以下代码



@ Html.LabelFor(model => model.contactNo,new {@class =control-label col-md-2})



@ Html.TextBoxFor(model => model.contactNo,new { @maxlength =10,onkeypress =return isNumberKey(event)})


@ Html.ValidationMessageFor(model => ; model.contactNo)





 <   script    类型  =  text / javascript > ;  

函数isNumberKey(evt){
var charCode =(evt.which)? evt.which:event.keyCode;
if(charCode!= 46&& charCode> 31
&&(charCode < 48 || charCode > 57))
返回false;

返回true;
}
< / script >









但它没有'工作。它仍然允许用户写字母并稍后验证

解决方案

在mvc中,您可以使用数据模型中的数据注释来控制输入的内容。

您将注释放在字段属性上方。

您可以使用常规快速注释来强制执行数字(即,[RegularExpression(@^ [0-9] *

)]),这不应该允许在该字段中输入除数字以外的任何其他内容。

您还可以使用范围注释来设置范围值,(即[范围(1,10)])。



你应该查看正则表达式。



您还有html帮助器来显示输入的验证消息(即文本框)。



在您的javascript中,您应该能够检查是否(charCode> = 48 && charCode< = 57),

这会增加约束以使其保持在0到9之间,你可以看到差异使用AND和OR之间。

你也可以在javascript中使用正则表达式。



最后,检查语法是否正确使用了引号参数。


在CSS3的某些版本中,您可以创建一个仅允许数字的INPUT项目 - 但并非所有浏览器都支持此项。



一个始终工作的方法是为输入框创建一个onchange事件。然后,每当输入(或删除)字符时,onchange事件的处理程序将遍历元素的值并丢弃任何非数字值。 [解决方案1中提出了一些执行此操作的方法]。



然后,使用结果更新输入框的值;




i have a problem with the code for locking the keyboard and only allowing numeric values to be input
i have been trying the following code in the view


@Html.LabelFor(model => model.contactNo, new { @class = "control-label col-md-2" })


@Html.TextBoxFor(model => model.contactNo, new { @maxlength = "10", onkeypress = "return isNumberKey(event)" })

@Html.ValidationMessageFor(model => model.contactNo)



<script type="text/javascript">

    function isNumberKey(evt) {
        var charCode = (evt.which) ? evt.which : event.keyCode;
        if (charCode != 46 && charCode > 31
          && (charCode < 48 || charCode > 57))
            return false;

        return true;
    }
</script>





but it doesn't work. it still allows the user to write letters and validate later

解决方案

In mvc you can use data annotations in your data model to control the content that gets entered.
You place the annotations above the field property.
You can use regular express annotation to enforce numbers only ( i.e., [RegularExpression(@"^[0-9]*


")] ), this should not allow anything else to be entered in that field other than numeric digits.
You can also use the range annotation to set the range of values, ( i.e., [Range(1, 10)] ).

You should check out regular expressions.

You also have html helpers to show validation messages for the input ( i.e., textbox ).

In your javascript, you should be able to just check if(charCode >= 48 && charCode <= 57),
this adds the contraints to keep it between 0 and 9, you can see the difference between using AND and OR.
You can also use regular expressions in javascript.

Finally, check the syntax for proper use of quotes on parameters.


In some versions of CSS3, you can create an INPUT item that only allows numbers - but not all browser will support this.

An always-works method would be to create an onchange event for the input box. Then, whenever a character is entered (or removed), you handler for the onchange event will go through the element's value and discard any non-numeric values. [Some methods for doing this were suggested in solution 1].

Then, use the result to update the input box's value;


这篇关于如何锁定键盘并仅允许将数字输入Mvc5中的Editorfor / Textbox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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