AJAX异步调用的登录验证JS

$(function(){
$('#login').click(loginAction);
$('#count').blur(checkName);
$('#password').blur(checkPassword);
});


function getKey()  
{  
    if(event.keyCode==13){  
    $('#login').click();
    }     
}


function checkPassword(){
var password = $('#password').val();
var rule =  /\S/;
if(! rule.test(password)){
$('#password').next().html("请输入密码");
return false;
}
$('#password').next().empty();
return true;
}


function checkName(){
var name = $('#count').val();
var rule = /^1\d{10}$/;
if(! rule.test(name)){
$('#count').next().html("手机号码");
return false;
}
$('#count').next().empty();
return true;
}


function loginAction(){
var name = $('#count').val();
var password = $('#password').val();

var n=checkName()+checkPassword();
if(n!=2){
return;
}

if(!checkName()){return;}

var data = {"username":name,"password":password};
$.ajax({
url:'user/login.do',
data:data,
type:'post',
dataType:'json',
success: function(result){
console.log(result);
if(result.state==0){
var user = result.data;
console.log(user);

location.href='/telephone_list/user/toLogin.do';
}else{
var msg = result.message;
$('#count').next().html(msg);
}
},
error: function(e){alert("通信失败!");}
})
}

你可能感兴趣的:(JS)