javascript 如何仅将数字分配给 MVC 视图中的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20762151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to assign only numbers to text box in MVC view
提问by Cassini
I have put a text box where i want only numbers. I want to validate it in the client side and written the following code
我在我只想要数字的地方放了一个文本框。我想在客户端验证它并编写以下代码
@using (Html.BeginForm("LoyaltyPoints","Cart",FormMethod.Post))
{
<label>Enter point:</label>
<br />
@Html.TextBoxFor(m => m.txtLoyaltyPoints, new { @onkeypress = "OnlyNumeric(this)" })
<br />
<input type="submit" value="Submit" />
<br />
@ViewBag.LoyaltyPointsErrorMessage
}
@Scripts.Render("~/bundles/jquery")
<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;
}
}
}
now here my javascript is not firing. i tried keeping alert here but not working as intended. What could be the error. please help.
现在在这里我的 javascript 没有触发。我试着在这里保持警惕,但没有按预期工作。可能是什么错误。请帮忙。
回答by Ranjith Kumar Nagiri
use the following code:
使用以下代码:
@Html.TextBoxFor(m => m.txtLoyaltyPoints, new {@id ="txtLoyalty" })`
<script type="text/javascript">
$(document).ready(function () {
$("#txtLoyalty").keydown(function (event) {
if (event.shiftKey) {
event.preventDefault();
}
if (event.keyCode == 46 || event.keyCode == 8) {
}
else {
if (event.keyCode < 95) {
if (event.keyCode < 48 || event.keyCode > 57) {
event.preventDefault();
}
}
else {
if (event.keyCode < 96 || event.keyCode > 105) {
event.preventDefault();
}
}
}
});
});
回答by zooblin
@Html.TextBoxFor(m => m.Height, new { @type = "number", @onkeypress = "ValidateNumber(event);" })
function ValidateNumber(event) {
var theEvent = event || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9]|\./;
if (!regex.test(key)) {
theEvent.preventDefault ? theEvent.preventDefault() : (theEvent.returnValue = false);
}
回答by Arindam Nayak
You can replace onkeypress with onblur
您可以用 onblur 替换 onkeypress
i.e.
IE
@Html.TextBoxFor(m => m.txtLoyaltyPoints, new { @onblur = "OnlyNumeric(this)" })
回答by Magesh Tamilselvan
<td class="field_nameW">
[email protected]("PayFromBankId3_Amount", 0.00M, new { @class = "amount_field", @onblur = "FormatAmountDecimal(this,3);", @size = "7", title = "Amount", @maxlength = "11" })
<script>
$('#Id').numeric({ allow: "." });
</script>
回答by Mohamed Khan
@Html.TextBoxFor(m => m.txtLoyaltyPoints, new { @onkeypress = "return onlyNos(event,this);" })
<script>
function onlyNos(e, t) {<br/>
if (window.event) {<br/>
var charCode = window.event.keyCode;<br/>
}<br/>
else if (e) {<br/>
var charCode = e.which;<br/>
}<br/>
else { return true; }<br/>
if (charCode > 31 && (charCode < 48 || charCode > 57)) {<br/>
alert("Please Enter only Numbers");<br/>
return false;<br/>
}<br/>
return true;<br/>
}
</script>