'how to apply numeric validation
I want to apply validation for integer but it's not working
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>
Solution 1:[1]
Maybe you can try to use 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;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Yiyi You |
