金额校验
input框输入金额区间,查找数据;校验金额合法性,保留两位小数
效果:
实现步骤:
xx.html
<input type="text" id="startMoney" name="startMoney" onkeyup="verify(this)" style="width:100px" />
<span>~</span>
<input type="text" id="endMoney" name="endMoney" onkeyup="verify(this)" style="width:100px;" /> 元
xx.js
// 限制输入的金额只能为数字,并且只能保留2位小数
function verify(obj){
obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");//限制"10.5." 这种情况
obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数
}
使用 JS 或 JQ 取值传到后台,略过。
xxMapper.xml
<!-- 金额区间 -->
<if test="startMoney != null and startMoney != ''">
and IFNULL(totalAmount,0) >= #{startMoney}
</if>
<if test="endMoney != null and endMoney != ''">
and #{endMoney} >= IFNULL(totalAmount,0)
</if>