gpt4 book ai didi

Javascript 文本字段验证 ||资金投入

转载 作者:行者123 更新时间:2023-12-03 07:59:43 26 4
gpt4 key购买 nike

我目前有此验证,但当多次按下时,输入有时会通过。

文本字段 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

我需要 0.50 分的增量,抱歉我的英语不好

最佳答案

如果将值解析为可以比较的数字,则可以消除一些复杂性。请参阅下面的评论

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>

关于Javascript 文本字段验证 ||资金投入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34646119/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com