输入框输入生日并验证

2017-11-08  本文已影响0人  wanpan__
<template>
<div style="width:180px;">
    <el-input v-model="birthday" placeholder="请输入生日 格式 1991-12-18" @change="birthdayFn" :maxlength="10"></el-input><br>
    <el-button type="primary" @click="searchBirthday">搜索</el-button>

</div>

</template>
data () {
            return {
                birthday: ''
            }
        },

methods: {
            birthdayFn() {
                console.log(this.birthday)
            },
            searchBirthday() {
                const re = /\d{4}-\d{1,2}-\d{1,2}/
                let open = false
                if (! re.test(this.birthday)) {
                    return void this.$message({
                        message: "请输入正确的生日格式",
                        type: 'warning'
                    })

                }
                let _this = this
                this.birthday.replace(/(\d{4})-(\d{1,2})-(\d{1,2})/, (match, year, month, day) => {
                    console.log(match, month, day)
                    if (year > new Date().getFullYear()) {
                        _this.$message({
                            message: "请输入正确的年份",
                            type: "warning"
                        })

                        open = true

                        return
                    }

                    if (month > 12) {
                        _this.$message({
                            message: "月份不能大于 12",
                            type: "warning"
                        })

                        open = true

                        return
                    }

                    if (day > 31) {
                         _this.$message({
                            message: "日期不能大于31",
                            type: "warning"
                        })

                        open = true

                        return
                    }
                })

                if (open) {
                    return
                }

                console.log("去搜索")

            },
}

// birthday: this.birthday.replace(/\b(\d)\b/g, "0$1") + " 00:00:00",
上一篇下一篇

猜你喜欢

热点阅读