我有 3 个文本框,其中包含邮政编码、手机号码和住宅号码。我得到了使用来自 Bellow post 的 jquery 在文本框中只允许数字的解决方案。
但是我们可以像使用 MVC4 razor 一样使用数据注释来做到这一点吗?
我有 3 个文本框,其中包含邮政编码、手机号码和住宅号码。我得到了使用来自 Bellow post 的 jquery 在文本框中只允许数字的解决方案。
但是我们可以像使用 MVC4 razor 一样使用数据注释来做到这一点吗?
我只是在玩 HTML5 输入类型=数字。虽然并非所有浏览器都支持它,但我希望它是处理特定类型处理(ex 的数字)的前进方式。用剃刀做很简单(例如VB)
@Html.TextBoxFor(Function(model) model.Port, New With {.type = "number"})
并感谢 Lee Richardson,c# 方式
@Html.TextBoxFor(i => i.Port, new { @type = "number" })
超出了问题的范围,但您可以对任何 html5 输入类型执行相同的方法
使用正则表达式,例如
[RegularExpression("([1-9][0-9]*)", ErrorMessage = "Count must be a natural number")]
public int Count { get; set; }
@Html.TextBoxFor(m => m.PositiveNumber,
new { @type = "number", @class = "span4", @min = "0" })
在带有 Razor 的 MVC 5 中,您可以按照上面的示例在匿名对象中添加任何 html 输入属性,以仅允许正数进入输入字段。
在文本框中写下这段代码onkeypress="return isNumberKey(event)"
和函数就在下面。
<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>
请使用DataType
属性。这将接受负值,因此下面的正则表达式将避免这种情况:
[DataType(DataType.PhoneNumber,ErrorMessage="Not a number")]
[Display(Name = "Oxygen")]
[RegularExpression( @"^\d+$")]
[Required(ErrorMessage="{0} is required")]
[Range(0,30,ErrorMessage="Please use values between 0 to 30")]
public int Oxygen { get; set; }
这对我有用:
<input type="text" class="numericOnly" placeholder="Search" id="txtSearch">
Java脚本:
//Allow users to enter numbers only
$(".numericOnly").bind('keypress', function (e) {
if (e.keyCode == '9' || e.keyCode == '16') {
return;
}
var code;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
if (e.which == 46)
return false;
if (code == 8 || code == 46)
return true;
if (code < 48 || code > 57)
return false;
});
//Disable paste
$(".numericOnly").bind("paste", function (e) {
e.preventDefault();
});
$(".numericOnly").bind('mouseenter', function (e) {
var val = $(this).val();
if (val != '0') {
val = val.replace(/[^0-9]+/g, "")
$(this).val(val);
}
});
在您的脚本中使用此函数并在文本框附近放置一个跨度以显示错误消息
$(document).ready(function () {
$(".digit").keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
$("#errormsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
});
@Html.TextBoxFor(x => x.company.ContactNumber, new { @class = "digit" })
<span id="errormsg"></span>
对于大于零的十进制值,HTML5 的工作方式如下:
<input id="txtMyDecimal" min="0" step="any" type="number">
can we do this using data annotations as I am using MVC4 razor ?
不,据我了解您的问题,不显眼的验证只会显示错误。最简单的方法是使用 jquery 插件:
这是允许您仅输入数字的javascript。
订阅onkeypress
文本框的事件。
@Html.TextBoxFor(m=>m.Phone,new { @onkeypress="OnlyNumeric(this);"})
这是它的javascript:
<script type="text/javascript">
function OnlyNumeric(e) {
if ((e.which < 48 || e.which > 57)) {
if (e.which == 8 || e.which == 46 || e.which == 0) {
return true;
}
else {
return false;
}
}
}
</script>
希望它可以帮助你。
也许您可以使用 [Integer] 数据注释(如果您使用 DataAnnotationsExtensions http://dataannotationsextensions.org/)。但是,这只会检查值是否为整数,而不检查是否已填写(因此您可能还需要 [Required] 属性)。
如果您启用不显眼的验证,它将在客户端验证它,但您还应该在您的 POST 操作中使用 Modelstate.Valid 来拒绝它,以防人们禁用了 Javascript。
嗨试试下面的......
<div class="editor-field">
<%: Html.TextBoxFor(m => m.UserName, new {onkeydown="return ValidateNumber(event);" })%>
<%: Html.ValidationMessageFor(m => m.UserName) %>
</div>
脚本
<script type="text/javascript">
function ValidateNumber(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
};
DataType
有第二个构造函数,它接受一个字符串。但是,在内部,这实际上与使用UIHint
属性相同。
DataType
由于枚举是 .NET 框架的一部分,因此无法添加新的核心 DataType 。您可以做的最接近的事情是创建一个继承自DataTypeAttribute
. 然后,您可以使用自己的DataType
枚举添加一个新的构造函数。
public NewDataTypeAttribute(DataType dataType) : base(dataType)
{ }
public NewDataTypeAttribute(NewDataType newDataType) : base (newDataType.ToString();
你也可以通过这个链接。但我会建议你使用 Jquery 来做同样的事情。
<input type="number" @bind="Quantity" class="txt2" />
使用 type="number"
@Html.TextBoxFor(x => x.MobileNo, new { @class = "digit" , @maxlength = "10"})
@section Scripts
{
@Scripts.Render("~/bundles/jqueryui")
@Styles.Render("~/Content/cssjqryUi")
<script type="text/javascript">
$(".digit").keypress(function (e) {
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57))
{
$("#errormsg").html("Digits Only").show().fadeOut("slow");
return false;
}
});
</script>
}
function NumValidate(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
alert('Only Number ');
return false;
} return true;
} function NumValidateWithDecimal(e) {
var evt = (e) ? e : window.event;
var charCode = (evt.keyCode) ? evt.keyCode : evt.which;
if (!(charCode == 8 || charCode == 46 || charCode == 110 || charCode == 13 || charCode == 9) && (charCode < 48 || charCode > 57)) {
alert('Only Number With desimal e.g.: 0.0');
return false;
}
else {
return true;
} } function onlyAlphabets(e) {
try {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123) || (charCode == 46) || (charCode == 32))
return true;
else
alert("Only text And White Space And . Allow");
return false;
}
catch (err) {
alert(err.Description);
}} function checkAlphaNumeric(e) {
if (window.event) {
var charCode = window.event.keyCode;
}
else if (e) {
var charCode = e.which;
}
else { return true; }
if ((charCode >= 48 && charCode <= 57) || (charCode >= 65 && charCode <= 90) || (charCode == 32) || (charCode >= 97 && charCode <= 122)) {
return true;
} else {
alert('Only Text And Number');
return false;
}}