对 input 千位分割
生活随笔
收集整理的這篇文章主要介紹了
对 input 千位分割
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
<template><div><div><el-form:rules="rules"ref="ruleForm"label-width="100px"class="demo-ruleForm":model="ruleForm"><el-form-item label="銷售額" prop="input"><el-input v-model="ruleForm.input" autocomplete="off"></el-input></el-form-item><el-form-item><el-button type="primary" @click="submitForm('ruleForm')">提交</el-button><el-button @click="resetForm('ruleForm')">重置</el-button></el-form-item></el-form></div><div>{{newInput}}</div></div>
</template><script>
export default {data() {var numValidate = (rule, value, callback) => {let numArr = value.split(".");let num1 = numArr[0];let num2 = numArr[1] ? numArr[1] : "";if (!/^[1-9]*[0-9]/.test(num1) && !/^[1-9]*[0-9]/.test(num2)) {callback(new Error("請輸入正數"));} else if (num2.length > 2) {callback(new Error("請保留小數點兩位數字"));} else if (Number(value) > 10e7 || Number(value) < 0) {callback(new Error("銷售額必須在0~" + 10e7 + "之間的數字"));} else {this.inputMoneyFomet(this.ruleForm.input, "input");callback();}};return {rules: {input: [{ required: true, message: "銷售額不能為空", trigger: "blur" },{ validator: numValidate, trigger: "blur" }]},ruleForm: {input: undefined},input: ""};},computed: {newInput() {return typeof Number(this.input);}},methods: {inputMoneyFomet(el, name) {this.ruleForm[name] = this.getInputValue(el);},// 金額添加千分位getInputValue(el) {let inputVal = el || "";return this.comdify(this.delcommafy(inputVal));},//去除千分位中的‘,’delcommafy(num) {if (!num) return num;num = num.toString();num = num.replace(/,/gi, "");return num;},comdify(n) {this.input = n;if (!n) return n;let str = n.split(".");let re = /\d{1,3}(?=(\d{3})+$)/g;let n1 = str[0].replace(re, "$&,");return str.length > 1 && str[1] ? `${n1}.${str[1]}` : `${n1}`;},// 獲取輸入框的值submitForm(formName) {this.$refs[formName].validate(valid => {if (valid) {alert("submit!");} else {console.log("error submit!!");return false;}});},resetForm(formName) {this.$refs[formName].resetFields();}}
};
</script>
總結
以上是生活随笔為你收集整理的对 input 千位分割的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 过年不让放炮,我用Python实现了10
- 下一篇: 相思曲