我想对整数进行验证,但是无效
function fnAllowNumeric() {
if ((event.keyCode < 48 || event.keyCode > 57) && event.keyCode != 8) {
event.keyCode = 0;
alert("Accept only Integer..!");
return false;
}
}
<td>
@Html.TextBoxFor(x => x.Items[i].ReceivedNumberOfCases, new { @class = "form-control text-right", @id="Received_"+ i ,@onkeyup = "outer.onChangeReceived("+ i +")" ,onkeypress="return fnAllowNumeric()"})
@Html.ValidationMessageFor(x=>x.Items[i].ReceivedNumberOfCases,null,new{@class="text-danger",@id="recivedCasesMessage_"+ i })
</td>
也许你可以尝试使用onblur
:
<input onblur="return fnAllowNumeric(this)">
js:
function fnAllowNumeric(t) {
if (($(t).val() < 48 || $(t).val() > 57) && $(t).val() != 8) {
$(t).val(0)
alert("Accept only Integer..!");
return false;
}
}