js 验证手机号、密码、短信验证码

/**
 * 参数较验
 *
 * */
var verification = {
    stop : false, //倒计时
    //验证手机号
    phone : function (tel, id) {
        if ("" == tel || !tel) {
            mui.toast('手机号不可以为空!');
        } else {
            var reg = /^0?1[3|4|5|7|8][0-9]\d{8}$/;
            //验证规则
            if (reg.test(tel)) return true;
            mui.toast("手机号错误!");
        }
        document.getElementById(id).focus();
        return false;
    },

    //验证密码(密码只能由数字和字母组成)
    password : function (w, id) {
        if ("" == w || !w) {
            mui.toast('请输入密码!');
        } else if (w.length < 6) {
            mui.toast('密码至少大于等于6位!');
        } else if (w.length > 20) {
            mui.toast('密码不能超过20位!');
        } else if (w) {
            var reg = /^[0-9a-zA-Z]+$/;
            if (reg.test(w)) return true;
            mui.toast("密码只能由数字和字母组成");
        }
        document.getElementById(id).focus();
        return false;
    },

    //验证码倒计时
    code : function (tel, btn, type) {
        var that = this,
            tel = $.trim(tel);
        if (!this.phone(tel, 'userTel')) return false;
        if (true == that.stop) return false; //防止重复点击
        that.stop = true;

        var btn = $("#"+btn);
        btn.attr("disabled", true).text("正在发送");
        var _no = 60;
        var time = setInterval(function () {
            _no--;
            btn.text(_no + "秒后重发");
            if (_no == 0) {
                //btn.attr("disabled", false).text("获取验证码");
                btn.removeAttr('disabled').text("重新获取验证码");
                that.stop = false;
                _no = 60;
                clearInterval(time);
            }
        }, 1000);

        var url = "/Home/User/sendVerifyCode.html";
        $.post(url, {
            toNumber: tel,
            type:type
        }, function (result) {
            mui.toast(result.info);
            if (200 != result.status) {
                btn.removeAttr('disabled').text("获取验证码");
                that.stop = false;
                _no = 60;
                clearInterval(time);
            }
        }, 'json');
    }
};

转载于:https://my.oschina.net/guoxin2014/blog/807352

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值