视图
<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;
}
}
}
yii 2.0 ajax判断 是否为手机号还是邮箱登陆
最新推荐文章于 2022-09-23 09:49:00 发布