2017-02-13 17 views
0

我想輸入的最大數字位數爲16包括。和十進制數 金額數量應該是最大13和一個「。」和2位小數 例如。 5656546355646.00 = 16個字符如何停止指數值,並做最大數字驗證

但是目前它沒有驗證並且正在顯示指數值。 13位最大,不僅要高達2位小數顯示整數,而不應顯示指數值

這裏是我的代碼 腳本

function getKeyValue(keyCode) { 
    if(keyCode > 57) { //also check for numpad keys 
     keyCode -= 48; 
    } 
    if(keyCode >= 48 && keyCode <= 57) { 
     return String.fromCharCode(keyCode); 
    } 
} 

function formatNumber(input) { 
    if(isNaN(parseFloat(input))) { 
     return "0.00"; 
    } 
    var num = parseFloat(input); 
    return (num/100).toFixed(2); 
} 

$(".amountInput").keydown(function(e) {  
       //handle backspace key 

       var input = $(this).attr("data-actual-input"); 
       if(e.keyCode == 8 && input.length > 0) { 
        input = input.slice(0,input.length-1); //remove last digit 
        $(this).attr("data-actual-input", input); 
        $(this).val(formatNumber(input)); 
       } 
       else { 
        var key = getKeyValue(e.keyCode); 
        if(key) { 
         input += key; //add actual digit to the input string 
         $(this).attr("data-actual-input", input); 
         $(this).val(formatNumber(input)); //format input string and set the input box value to it 
        } 
       } 
       if(e.which == 8 || e.which == 46) { 
        return true; 
       } 
       var inpVal = $(this).val(); 
       if(inpVal.length > 15) { 
       e.preventDefault(); 
       return false; 
       } 
       return false; 
      }); 

HTML代碼

<input type='tel' class="form-control amountInput" name="amt" data-actual-input="" value="0.00" required> 

jsfiddle

https://jsfiddle.net/Lpn1w84f/1/

回答

0

你試過maxlength屬性?從你的例子:

<input type='tel' class="form-control amountInput" name="amt" data-actual-input="" value="0.00" maxlength="15" required> 
+0

它不會工作,嘗試的jsfiddle .... – Aryan

+0

最大長度的作品,問題是該keydown()函數。要看到這一點,只需禁用此功能(例如返回true)。 (「。amountInput」)。keydown(function(e){ return true; }); –