一个表单验证的问题,如何通过获取当前校验得文本框ID进而使用相应JSON键值的问题,非常感谢

<div class="rol">
    手机:
    <input type="text" value="" id="mobile">
    <span class="tips" style="display: none">提示文字</span>
</div>

function checkAction(){
    this.addStyle = function(){
        $(this).addClass("focus");
    };

    this.removeStyle = function(){
        $(this).removeClass("focus");
    }
}



var checkItem = {
    mobile : {
        reg : "/^1[3|5][0-9]\d{4,8}$/",
        error: "手机号格式错误",
        tip:"请输入手机号用户密码找回"
    },
    email:{
        reg:"",
        error:"邮件格式错误",
        tip:"请输入邮件地址"
    }

};


$(function(){

    // 手机验证
    $('input[type=text]').bind('focus', function () {
        checkAction.call(this);
        this.addStyle();

    });

    $('input[type=text]').bind('blur', function () {
        checkAction.call(this);
        this.removeStyle();
        var getId = this.id;//获得当前文本框ID

        if ( this.value == '' ){
            $(this).next('span').css('display','block');
        } else {
            if( !(checkItem.getId.reg.test(this.value)) ){//我的问题是:通过checkItem.mobile.reg可以读到mobile的json字典表的值,那么这里我想动态的判断是mobile这个ID表单项,进而通过checkItem.getId.reg也同样能读到mobile相应的键值,有劳大牛指导,谢谢
                alert(checkItem.mobile.error);
            } else {
                alert("输入正确");
            }
        }


    });

});
阅读 2.8k
1 个回答

checkItem.getId.reg.test(this.value)这句有问题
改成
checkItem[getId].reg.test(this.value)就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题