美文网首页
输入框输入生日并验证

输入框输入生日并验证

作者: wanpan__ | 来源:发表于2017-11-08 18:13 被阅读0次
    <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",

    相关文章

      网友评论

          本文标题:输入框输入生日并验证

          本文链接:https://www.haomeiwen.com/subject/clvymxtx.html