如何允许mvc4剃须刀在文本框中只有数字 [英] How to allow only numbers in textbox in mvc4 razor
问题描述
我有3个文本框其取值邮政code&放;手机号码和放大器;住宅电话。
我得到了解决方案,使用jQuery从贝娄后允许文本框中只数。
I have 3 text box which takes values postal code & mobile number & residential number. I got the solution for allowing only number in text box using jquery from Bellow post.
<一个href=\"http://stackoverflow.com/questions/7310796/i-would-like-to-make-an-editfor-textbox-accept-numbers-only\">I想作一个EditFor文本框接受数字只
但我们可以做到这一点使用数据注解我使用MVC4剃须刀?
but can we do this using data annotations as I am using MVC4 razor ?
推荐答案
我只是用HTML5输入类型玩弄=号。而其不是所有的浏览器都支持我希望它是向前来处理特定类型的处理(为前数)的方式。 pretty简单剃刀做(当然是VB)
i was just playing around with HTML5 input type=number. while its not supported by all browsers I expect it is the way going forward to handle type specific handling (number for ex). pretty simple to do with razor (ex is VB)
@Html.TextBoxFor(Function(model) model.Port, New With {.type = "number"})
和感谢李·理查森,C#的方式
and thanks to Lee Richardson, the c# way
@Html.TextBoxFor(i => i.Port, new { @type = "number" })
超出了问题的范围,但你可以做任何HTML5输入类型同样的方法
beyond the scope of the question but you could do this same approach for any html5 input type
这篇关于如何允许mvc4剃须刀在文本框中只有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!