2016-09-22 12 views
1

我是javascript中的新成員,我想從javascript.textbox驗證文本框應僅帶數字值,並且不應在起始位置爲零。textbox只帶數字值,並且在起始處不爲零

我試過下面的函數可以正常工作,但不知道如何避免零位從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; 
} 

請幫我一把。

回答

1

你可以用它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; 
} 
相關問題