支付金额正则

2019-08-09  本文已影响0人  曹锦花
<template>
    <div class="con">
        <div class="header">商户名称</div>

        <div class="num">
            输入金额
            <div class="input">{{amount}}</div>
            <div class="rmb">¥</div>
            <div class="animation"></div>
        </div>

        <van-number-keyboard
            :show="show"
            theme="custom"
            extra-key="."
            close-button-text="完成"
            @input="onInput"
            @delete="onDelete"
        />
    </div>
</template>
__________________________输入校验方法
    onInput(value) {
            this.amount = this.amount + value;
            this.amount = (this.amount).toString()
            let regu = /^(0|([1-9]\d{0,4}))([\.]|\.\d{1,2})?$/;
            let re = new RegExp(regu);
            if (re.test(this.amount)) {
                console.log(true)
            } else {
                console.log(false)
                if(value != 0 && value != '.' && this.amount.length == 2){
                    this.amount = this.amount.substr(1)
                } else {
                    let arr = this.amount.split('')
                    let pointArr = []
                    arr.forEach((item, index) => {
                        if(item == '.') {
                        pointArr.push(item) 
                        }
                    })
                    if(this.amount.length > 1) {
                        this.amount = this.amount.slice(0,this.amount.length - 1)
                    }
                }
            }
        },
————————————————————删除方法
        onDelete() {
            this.amount = this.amount.substr(0, this.amount.length - 1);
        }
上一篇 下一篇

猜你喜欢

热点阅读