利用正则表达式和Ajax验证登录

主要实现于登录成功,页面跳转,登录失败,弹窗提示,如果输入的用户名和密码符合正则表达式的规则,输入框后面会出现红色x,反之出现绿色对号

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录页面</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script type="text/javascript" src="js/jquery.js"></script>
	<style>
		html{   
		    width: 100%;   
		    height: 100%;   
		    overflow: hidden;   
		    font-style: sans-serif;   
		}   
		body{   
		    width: 100%;   
		    height: 100%;   
		    font-family: 'Open Sans',sans-serif;   
		    margin: 0;   
		    background-color: #4A374A;   
		}   
		#login{   
		    position: absolute;   
		    top: 50%;   
		    left:50%;   
		    margin: -150px 0 0 -150px;   
		    width: 300px;   
		    height: 300px;   
		}   
		#login h1{   
		    color: #fff;   
		    text-shadow:0 0 10px;   
		    letter-spacing: 1px;   
		    text-align: center;   
		}   
		h1{   
		    font-size: 2em;   
		    margin: 0.67em 0;   
		}   
		input{   
		    width: 278px;   
		    height: 18px;   
		    margin-bottom: 10px;   
		    outline: none;   
		    padding: 10px;   
		    font-size: 13px;   
		    color: #fff;   
		    text-shadow:1px 1px 1px;   
		    border-top: 1px solid #312E3D;   
		    border-left: 1px solid #312E3D;   
		    border-right: 1px solid #312E3D;   
		    border-bottom: 1px solid #56536A;   
		    border-radius: 4px;   
		    background-color: #2D2D3F;   
		}   
		.but{   
		    width: 300px;   
		    min-height: 20px;   
		    display: block;   
		    background-color: #4a77d4;   
		    border: 1px solid #3762bc;   
		    color: #fff;   
		    padding: 9px 14px;   
		    font-size: 15px;   
		    line-height: normal;   
		    border-radius: 5px;   
		    margin: 0;   
		}  
	</style>
  </head>
  
  <body>
    <div id="login">  
        <h1>Login</h1>  
        <form method="post">  
            <input type="text" required="required" placeholder="用户名" name="name" id="name" onblur="check_name()"></input><span id="nameReg"></span>  
            <input type="password" required="required" placeholder="密码" name="pwd" id="pwd" onblur="check_pwd()"></input><span id="pwdReg"></span>
            <button class="but" type="submit">登录</button>  
        </form>  
    </div>
    <script type="text/javascript">
    	//js
    	function check_name() {
	      console.log(1);
	      //获取账号
	      var code = document.getElementById("name").value;
	      //校验其格式(\w字母或数字或下划线)
	      var span = document.getElementById("nameReg");
	      var reg = /^[a-zA-Z]{1}([a-zA-Z0-9]|[._]){4,19}$/;
	      if(reg.test(code)) {
	        //通过,增加✓样式
	        span.innerText = "✓";
	        span.style.color = "green";
	      } else {
	        //不通过,增加✗样式
	        span.innerText = "✗";
	        span.style.color = "red";
	      }
	   }
    function check_pwd(){
       console.log(2);
       var code2 = document.getElementById("pwd").value;
       var span2 = document.getElementById("pwdReg");
       var reg2 = /^(\w){6,20}$/;
       if(reg2.test(code2)) {
	        span2.innerText = "✓";
	        span2.style.color = "green";
	      } else {
	        span2.innerText = "✗";
	        span2.style.color = "red";
       } 
    }	
    $(function(){
    	$(":button").on("click",function(){
    		$.ajax({
    			url:"test",
    			type:"post",
    			data:{
    				name:$("#name").val(),
    				pwd:$("#pwd").val()
    			},
    			dataType:"text",
    			success:function(data){
    				if(data == "ok"){
    					window.location.href = "test.jsp";
    				}else{
    					alert("用户名或密码输入错误,请重新输入!");
    				}
    			}
    		});
    	});
    });
    </script> 
  </body>
</html>

AjaxServlet代码的url为“test”

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AjaxServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//自定义用户名和密码可更改
		String name = "admin";
		String pwd = "123";
		String nameAjax = request.getParameter("name");
		String pwdAjax = request.getParameter("pwd");
		if (name.equals(nameAjax) && pwd.equals(pwdAjax)) {
			out.print("ok");
		}else{
			out.print("error");
		}
		out.flush();
		out.close();
	}
}

登陆成功跳转的页面test.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>登录成功</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <p>这是登录成功后的页面</p>
  </body>
</html>

效果演示
格式错误
在这里插入图片描述
格式正确
在这里插入图片描述
用户名和密码输入错误
在这里插入图片描述
用户名和密码输入正确,跳转成功
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值