首页 > 代码库 > 问答项目---登陆验证码点击切换及异步验证验证码

问答项目---登陆验证码点击切换及异步验证验证码

输出验证方法:

public function verify(){
    $config = array(
        ‘length‘ => 2,
        ‘reset‘ => false,
        ‘useCurve‘  =>  false,
        ‘useNoise‘  =>  false,
    );
    $obj = new \Think\Verify($config);
    $obj->entry();
}

调用:

<img src="http://www.mamicode.com/{:U(‘verify‘)}"/ id=‘code‘>
<a href="javascript:void(change_code(this));">看不清</a>

输入路径:

<script type=‘text/javascript‘>
var CONTROL = ‘__URL__‘;
</script>

点击切换验证码:

function change_code(obj){
    $("#code").attr("src", CONTROL+"/verify/"+Math.random());
    return false;
}

异步验证验证码方法:

/* 异步验证验证码 */
public function checkVerify(){
    $code = I(‘post.code‘);
    $config = array(
        ‘reset‘ => false,//不重置
    );   
    $obj = new \Think\Verify($config);
    if($obj->check($code)){
        echo "1";
    }else{
        echo "0";
    };
}

异步验证验证码JS:

//验证验证码
$("input[name=‘code‘]").blur(function(){
    var code = $("input[name=‘code‘]");
    if(code.val().trim()==‘‘){
        code.parent().find("span").remove().end().append("<span class=‘error‘>验证码不能为空</span>");
        return ;
    }
    $.post(CONTROL+"/checkVerify",{code:code.val().trim()},function(stat){
        if(stat==1){
            validate.code=0;
            code.parent().find("span").remove();
        }else{
            code.parent().find("span").remove().end().append("<span class=‘error‘>验证码错误</span>");
        }

    })
})

 

问答项目---登陆验证码点击切换及异步验证验证码