我是 javascript 的新手,我想验证 javascript 中的文本框。文本框应该只取数值,而不应该在开始时取零。
我尝试了以下功能,但效果很好,但不明白如何避免 First Position 为零。
下面是我的html代码。
<asp:TextBox ID="cp1_txtMob" palceholder="10 digit mobile No." ondrop="return false;" onPaste="return false;" runat="server" class="textfile_new2" Style="color: #333;" onkeydown="return isNumberKey(event);" MaxLength="10" autocomplete="OFF"></asp:TextBox>
function isNumberKey(e) {
var t = e.which ? e.which : event.keyCode;
if ((t >= 48 && t <= 57) || (t >= 96 && t <= 105) || (t == 8 || t == 9 || t == 127 || t == 37 || t == 39 || t == 46))
return true;
return false;
}
请帮帮我。
最佳答案
你可以像onkeyup="AvoidZero(this.id);"
那样使用它
<asp:TextBox ID="cp1_txtMob" palceholder="10 digit mobile No." ondrop="return false;" onPaste="return false;" runat="server" class="textfile_new2" Style="color: #333;" onkeydown="return isNumberKey(event);" MaxLength="10" autocomplete="OFF" onkeyup="AvoidZero(this.id);"></asp:TextBox>
function AvoidZero(v) {
var V = document.getElementById(v).value;
return (V.charAt(0) == 0) ? (document.getElementById(v).value = V.substring(1, V.length), false) : false;
}
关于javascript - 文本框只取数值,不在开始时取零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39636968/