登陸用戶名密碼驗證

採用ajax與jquery結合的方式進行驗證處理
js:

<script type="text/javascript"> 

 $(function(){
 $(":input[name='btnsubmit']").click(function(){
    var flag = true;
    //1.獲取文本框中的值 
    var obj={};
    var username = $("input[name=username]").val(); 
    var password = $("input[name=password]").val(); 
    var url="loginAction_login.action";
    obj.username=username;
    obj.password=password;
    $.post(url,obj,function(date){
        if(date==1){
            flag=false;
            $("input[name=username]").focus(function(){
            $("input[name=username]").css("background-color","red");
            }).focus();
            $("#div1").html("用戶名不存在");
            return flag;
        }else if(date==2){
            flag=false;
            $("input[name=password]").focus(function(){
            $("input[name=password]").css("background-color","red");
            }).focus();
            $("#div2").html("密碼錯誤");
            return flag;
        }else{

            window.location = date;
            return flag;
        }
    });  
});

 });

</script> 

html:

<div class="right_">
                    <div class="show">
                        <label>用戶名</label><input type="text" class="text" name="username"
                            placeholder="用戶名"><span id="div1" style="color: red"></span>
                    </div>
                    <div class="show">
                        <label>密 &nbsp;&nbsp; 碼</label><input type="password"
                            name="password" class="text" placeholder="密碼"
                            onkeydown="KeyDown(event)">
                            <span id="div2" style="color: red"></span>
                    </div>
                    <div class="show color">
                        <label> &nbsp;</label> <input type="checkbox" id="rpwd"><label
                            class="simple" for="rpwd" style="text-align:left;">記住密碼</label> <a
                            href="#" style="color: gray">註冊</a> <a href="#"
                            style="color: gray">忘記密碼</a>
                    </div>
                    <div class="show">
                        <label> <img src="themes/images/home/barcode.jpg"
                            height="100px"></img>
                        </label> <input type="button" name="btnsubmit"  class="submit"value="登錄"
                            > 
                    </div>
                </div>

“`
action:

public String login() throws IOException{
String username = ServletActionContext.getRequest().getParameter(“username”);
String password = ServletActionContext.getRequest().getParameter(“password”);
ServletActionContext.getRequest().setCharacterEncoding(“UTF-8”);
ServletActionContext.getResponse().setContentType(“text/html;charset=UTF-8”);
PrintWriter out = ServletActionContext.getResponse().getWriter();
if(userService.getUsername(username)){
if(userService.getPassword(username, password)){
user=userService.findUserByName(username,password);
SessionContext.setUser(user);
out.print(“loginAction_success.action?type=3”);
return null;
}else{
out.print(“2”);
}
}else{
out.print(“1”);
}
return null;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章