javascript 类单页面的 信息提交 验证码

单页面的 信息提交 验证码
javascript 类

class Form {
    constructor(btn, name, phone, code, getCode) {
        this.btn = document.getElementsByClassName(btn)[0],
            this.name = document.getElementsByClassName(name)[0],
            this.phone = document.getElementsByClassName(phone)[0],
            this.code = document.getElementsByClassName(code)[0],
            this.getCode = document.getElementsByClassName(getCode)[0],
            this.reg = /^1[3456789]\d{9}$/,
            this.zz = /^[\u4e00-\u9fa5]{2,}$/,
            this.numt = 0,
            this.timer = null,
            this.nums = 60,
            this.referer = window.location.href,
            this.url = 'rqtt002';
        this.address = '/shqr'
        this.init();
    }
    init() {
        this.getCode.addEventListener('click', this.sendCode.bind(this))
        this.btn.addEventListener('click', this.sendMesg.bind(this))
    }
    sendCode() {
        let namevalue = this.name.value,
            phonevalue = this.phone.value;
        console.log(namevalue, phonevalue);
        if (namevalue == "" || !this.zz.test(namevalue)) {
            alert('请正确填写您的姓名')
        } else if (phonevalue == "" || !this.reg.test(phonevalue)) {
            alert('请正确填写手机号')
        } else {
            if (this.numt == 0) {
                sessionStorage.setItem('phonevalue', phonevalue);
                $.post(`${this.address}/src/sendSms.php`,
                    { 'phone_num': sessionStorage.getItem("phonevalue") }, result => {
                        if (result.indexOf('OK') !== '-1') {
                            alert('验证码已发送!请注意查收');
                            this.numt++;
                            this.timer = setInterval(() => {
                                this.nums--;
                                if (this.nums <= 0) {
                                    clearInterval(this.timer)
                                    this.numt = 0;
                                    this.nums = 60;
                                }
                            }, 1000)
                        } else {
                            alert('验证码发送失败,请稍后再试!')
                        }
                    });

            } else {
                alert(`验证码已发送,请${this.nums}秒后再试!`)
            }
        }


    }
    sendMesg() {
        let sendname = this.name.value,
            sendphone = this.phone.value,
            sendcode = this.code.value;
        if (sendname !== '' && sendphone !== '') {
            let sessionphone = sessionStorage.getItem('phonevalue');
            if (!this.zz.test(sendname)) {
                alert('请正确输入姓名')
            } else if (sendphone !== sessionphone) {
                alert('所填手机号与接收验证码手机号不符,请修改')
            } else {
                $.post(`${this.address}/src/codeTest.php`, { code: sendcode }, res => {
                    if (res == '1') {
                        $.post('save.php', { tel: sendphone, name: sendname, url: this.url, referer: this.referer }, data => {
                            if (data == '1') {
                                console.log(data);
                                alert('信息已提交,稍后会有专业人员与您取得联系');
                                meteor.track('form', { convert_id: 1681779814438923 })
                            } else if (data == '2') {
                                console.log(data);
                                alert('请勿重复提交!')
                            } else {
                                console.log(data);
                                alert('网络繁忙请稍后再试。')
                            }
                        })
                    } else {
                        alert('验证码不正确!')
                        return false
                    }
                });
            }
        } else {
            alert('信息填写有误!请正确填写相关信息!')
        }
    }



}
new Form('in_btn', 'in_name', 'in_phone', 'in_code', 'in_getCode')
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值