Files

95 lines
2.1 KiB
JavaScript
Executable File

module.exports = {
removeLetter:function(str){
let result;
let reg = /[a-zA-Z]+/; //[a-zA-Z]表示匹配字母,g表示全局匹配
while (result = str.match(reg)) { //判断str.match(reg)是否没有字母了
str = str.replace(result[0], ''); //替换掉字母 result[0] 是 str.match(reg)匹配到的字母
}
return str;
},
judgeFloat:function(qty){
let checkQtyErrorFlag = true;
let reg = /\D/;
checkQtyErrorFlag = reg.test(qty);
if(checkQtyErrorFlag) {
let firstQty = qty.toString().substr(0,1);
checkQtyErrorFlag = reg.test(firstQty);
//防止非数字开头
if(!checkQtyErrorFlag) {
let replaceQty = qty.toString().replace(".", "");
checkQtyErrorFlag = reg.test(replaceQty);
}
}
//排除0000.1
if(!checkQtyErrorFlag) {
if (qty.toString().substr(0,1) == "0" && qty.toString().substr(1,1) != "."){
checkQtyErrorFlag = true;
}
}
//排除0...1
if(!checkQtyErrorFlag) {
if ((qty.toString().split('.')).length > 2){
checkQtyErrorFlag = true;
}
}
if(checkQtyErrorFlag) {
uni.showToast({
icon: "none",
title: "请输入正确的数量值",
duration: 3500,
})
return false;
}
//排除0...1
if(!checkQtyErrorFlag) {
if ((qty.toString().split('.')).length > 1){
if (qty.toString().split('.')[1].length > 6){
checkQtyErrorFlag = true;
}
}
}
if(checkQtyErrorFlag) {
uni.showToast({
icon: "none",
title: "小数位最多只能输入6位",
duration: 3500,
})
return false;
}
return true;
},
judgeInt:function(qty){
let checkQtyErrorFlag = true;
let reg = /\./;
checkQtyErrorFlag = reg.test(qty);
if(checkQtyErrorFlag) {
return false;
}
return true;
},
round: function(num, decimal = 2) {
num = num.toString();
const index = num.indexOf('.')
if (index !== -1) {
num = num.substring(0, decimal + index + 1)
} else {
num = num.substring(0)
}
//截取后保留两位小数
return parseFloat(num).toFixed(decimal)
}
}