index.vue
1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<template>
<el-input :value="value" v-bind="$attrs" v-on="{...$listeners}" @blur="blurFn"></el-input>
</template>
<script>
export default {
name: 'FormatInput',
props: ['value','precise','intDigit','maxVal','minVal'],
data() {
return {
}
},
watch:{
value:{
handler(nVal, oVal){
this.inputFn(nVal)
},
deep: true
}
},
mounted(){
},
methods:{
inputFn(e){
e = e || e==0 ? String(e) : ''
this.intDigit = this.intDigit || 6
this.precise = this.precise || this.precise==0 || this.precise=='0' ? this.precise : 2
let value = e
if(this.maxVal && +this.maxVal<= +e){
value = this.maxVal //加等号是因为 '500' == '500.' 导致多了点
}else{
const intDigitReg = this.intDigit == -1 ? `(^\\d+)(\\.\\d+)?` : `(^\\d{${this.intDigit}})\\d+(\\.\\d+)?`
value = e.replace(/^0\d/g, '0').replace(/[^\d.]/g, '').replace(/^\./g, '').replace(new RegExp(intDigitReg,'g'), '$1').replace('.', '$#$')
.replace(/\./g, '').replace('$#$', '.').replace(new RegExp(`^(-)*(\\d+)\\.(\\d{${this.precise}}).*$`), this.intDigit == -1 ? '$1$2' : '$1$2.$3')
}
this.$emit('input', value)
},
blurFn(e){
let value = e.target.value
if(this.minVal && +this.minVal>=value) value = this.minVal
if(value && value.charAt(value.length-1)=='.') value = value.substr(0 ,value.length-1)
this.$emit('input', value)
}
}
}
</script>
<style lang="scss" scoped>
</style>