繁体   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