1. 程式人生 > >yii 2.0 ajax判斷 是否為手機號還是郵箱登陸

yii 2.0 ajax判斷 是否為手機號還是郵箱登陸

檢視
<input type="text" id="email" name="email" value="" tabindex="1" placeholder="請輸入登入郵箱地址" />
<input type="password" id="password" name="password" tabindex="2" placeholder="請輸入密碼" />
<input type="button" id="submit"    class="submitLogin" style="color:#fff;"  value="登     錄"/>
ajax判斷
  //登陸  判斷手機號或者郵箱
    $(function(){
        $("#submit").click(function(){
            var email=$("#email").val();
          //  alert(email);
            var password=$("#password").val();
            var phone=/^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
            var myreg = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
            if(email!=''){
                if(!password=='') {
                    if (myreg.test(email)) {
                        $.post("?r=login/select", {email: email, password: password, p: 1}, function (asg) {
                           if(asg==1){
                               alert("郵箱登陸成功");
                           }else{
                               alert("失敗");
                           }
                        })
                    } else if (phone.test(email)) {
                        $.post("?r=login/select", {phone: email, password: password, p: 2}, function (asg) {
                            if(asg==1){
                                alert("手機登陸成功");
                            }else{
                                alert("失敗");
                            }
                        })
                    } else {
                        alert("郵箱或者手機格式不對");
                        return false;
                    }
                }else{
                    return false;
                }
            }else{
                alert("郵箱不能空");return false;
            }
        })
    })
控制器
      public  function actionSelect(){
            $p=\yii::$app->request->post('p');
            if($p==1){
                $email=\yii::$app->request->post('email');
                $password=\yii::$app->request->post('password');
                $result = Register::find()->where(['email' => $email] and ['password'=>$password])->asArray()->one();
                if($result){
                    echo 1;
                }else{
                    echo 0;
                }
            }else{
                $phone=\yii::$app->request->post('phone');
                $password=\yii::$app->request->post('password');
                $result = Register::find()->where(['phone' => $phone] and ['password'=>$password])->asArray()->one();
                if($result){
                    echo 1;
                }else{
                    echo 0;
                }
             }
        }