簡體   English   中英

Javascript文本字段驗證|| 資金投入

[英]Javascript text field validation || Money input

我目前有這個驗證,但是當多次按下時,輸入有時會通過。

文字欄位ID:服務費率金額

$('#service-rate-amount').keyup(function() {
               var val = $(this).val();
               var checkIf50or00cents = new RegExp("^[0-9]+(\.([0,5]{1})?([0]{1})?)?$");
               var limitDigits = new RegExp("/^\d{0,3}(\.\d{1,2})?$/");

               if(isNaN(val) && val != "."){
                   showMessageModal("Only numeric characters are accepted");
                    val = val.replace(/[^0-9\.]/g,'');
                    if(val.split('.').length>2) {
                        val = val.replace(/\.+$/,"");
                   showMessageModal("Only one decimal point is accepted");
                    }
               } else if (!checkIf50or00cents.test(val)){
                   val = val.slice(0,-1);
                   showMessageModal("Only 50 cents or 00 cents are allowed");
               }          


               if (!(/^\d{0,3}(\.\d{1,2})?$/.test(val))) {
                   if(val.length == 4){
                       if( val.charAt(3) != "."){
                           val = val.slice(0, -1);
                           showMessageModal("Only three digits before decimal point is accepted");
                       }
                   }
               }

               $(this).val(val); 
            });

我想要一個輸入字段,將接受.... 00.50到999.50

我需要以.50 centavos為增量,對不起,我的英語不好

如果將值解析為可以比較的數字,則可以消除一些復雜性。 見下面的評論

 var dollars = document.querySelector('#dollars'); function checkDollarInput(){ var value = parseFloat( dollars.value ), valueInt = parseInt( value ); if( dollars.value.match(/[^0-9.-]/) ){ showMessageModal('please use only numbers', value); return false; } // check the max value if( value > 999.5 ){ showMessageModal('over max value', value); return false; } // check the min value if( value < 0.5 ){ showMessageModal('under min value', value); return false; } // ensure the correct decimal using modulo division remainer if( value % valueInt !== 0 && value % valueInt !== .5 ){ showMessageModal('needs to be .0 or .50', value ); return false; } console.log( 'success', value ); // all tests have passed return true; } // im only logging value for the sake of testing function showMessageModal( message, value ){ console.log.apply(console, arguments); // do something to show the modal } 
 <script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script> I want to have a input field that will accept .... 00.50 up to 999.50 <form id="form"> <input id="dollars" type="text" value="40.50" /> <input id="submit" type="button" value="checkDollarInput" onclick="checkDollarInput()" /> </form> 

您可以通過使用number類型的input來過濾掉所有文本輸入。

的HTML

<input id="validate" type="number">

的JavaScript

document.getElementById("validate").addEventListener("keyup", function(e) {
    var v = Number(this.value);
  if(v >= 0.5 && v <= 999.5) {
    var decimals = (v % 1).toFixed(2);
    if(decimals == 0 || decimals == 0.5) {
        console.log("Acceptable");
    }
  }
}, false);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM