2016-05-12 78 views
1

我有textbox用戶只能插入numbers。於是,我試着用下面的鏈接不允許小數進入文本框不起作用

JS FIDDLE

但還是我能夠插入[](十進制)在文本框中。下面是我試過

$(".allownumericwithoutdecimal").on("keypress keyup blur",function (event) {  
     $(this).val($(this).val().replace(/[^\d].+/, "")); 
     if ((event.which < 48 || event.which > 57)) { 
      event.preventDefault(); 
     } 

文本框

<igtxt:WebNumericEdit ID="txtNoofPages" Width="24%" runat="server" CssClass="allownumericwithoutdecimal"> 
      </igtxt:WebNumericEdit> 
+0

http://stackoverflow.com/questions/9204783/how-do-i-validate-a-decimal-field-with-jquery-and-regex – MusicLovingIndianGirl

+1

與鍵輸入瞎搞不是用戶友好的,如果該值看起來無效並且讓用戶自己修復,那麼只顯示屏幕提示會更好。嗅探鍵不會阻止無效值被粘貼,或者拖放到字段中,哦,'$(this).val()'是執行this.value的非常低效的方法。 – RobG

+0

@ MusicLovingIndianGirl:你給出的鏈接接受'.'小數。我不想要那個.. – BNN

回答

1

我想你,做工精細與Firefox,IE和Chrome瀏覽器JS提琴。然後我試着用jQuery與正常工作正常的相同的正則表達式。

$("#onlyNumber").on('keypress keyup blur', function(){ 
 
    $(this).val($(this).val().replace(/[^\d].+/, "")); 
 
     if ((event.which < 48 || event.which > 57)) { 
 
      event.preventDefault(); 
 
     } 
 
    
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<label>Number Only</label> 
 
<input type='textbox' id='onlyNumber'>

+0

這不可能是答案,而是一個確認OP代碼正常工作的評論。 – niksofteng

+1

已嘗試,但無法正常工作 – BNN

+0

請參見[WorksForMe](http://meta.stackexchange.com/questions/118992/are-works-for-me-answers-valid) –

相關問題