表单校验的二三事

1,什么会触发表单校验

(a)文本框失去焦点

(b)点击按钮

(c)提交表单

 

2,有哪几种校验方式

(1)同步方式

为空判断,格式判断(包括长度判断)

(2)异步方式

通过ajax(获取jsonp)调用后台接口来校验

 

下面的校验就会有问题

inputs.push(
            new TextBox(Cjt.byId('identityTF'), {
                validate: function () {
                    if (this.el.value.length == 0) {
                        tip.error(me._error.mobile_is_null);
                        return false;
                    }else if (!util.isMobileNo(this.el.value)){
                        tip.error(me._error.mobile_Format_wrong);
                        return false;
                    }else{
                        Cjt.get('/register/findUserIdentify', {uid: this.el.value}, function (resp) {
                            console.log(resp);
                            if(resp.exists && resp.exists==1){
                                tip.error("该手机已注册,您可直接<a href='/wap/login.html' >登录</a>");
                                return false;
                            }
                        });
                    }
                    return true;
                }
            })
        );

 问题:在方法返回之后,ajax还没有执行完毕

解决方法:

common.findUserIdentify(mobileVal, function () {
                tip.error("该手机已注册,您可直接<a href='/wap/login.html' >登录</a>");
                return false;
            }, function () {
                console.log('开始发送短信');
                Cjt.post('/wap/bindMobile', data, function (resp) {
                    console.log(resp);
                    if (resp.result) {
                        tip.success('验证码已发送,请查收');
                        sendCodeBtn.disable($(node));
                        me._data.bind.secondCount = 10;
                        console.log(me._data);
                        me._data.bind.intervalHook = setInterval(SMSbtnTiming, 1000);
                    } else {//发送短信验证码成功,开始倒计时
                        tip.error(resp.errorMessage);
                    }
                })
            })

 

 

你可能感兴趣的:(表单校验,校验表单,输入框校验)