Reputation: 207
I am new in javascript and i want to validate textbox from javascript.textbox should take only numeric value and should not take zero at the starting.
I tried below function that works fine but not understand how to avoid zero from First Position.
below is my html code.
<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;
}
Please help me out.
Upvotes: 1
Views: 294
Reputation: 898
you can use it onkeyup="AvoidZero(this.id);"
like
<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;
}
Upvotes: 1
Reputation: 1402
Ideally you should use isNaN() for such comparisions.
http://www.w3schools.com/jsref/jsref_isnan.asp
Upvotes: 1