使用ajax与正则表达式实现验证登录

代码:

<%@ 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>My JSP 'index.jsp' starting page</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 type="text/css">

input{
width:160px;
height:25px;
text-align: center;
border-collapse: collapse;
}
</style>
</head>
<body>
	<form>
		账号:<input type="text" name="name" id="name" onbler="Name" /><br />
		密码:<input type="text" name="pwd" id="pwd" onbler="Pwd" /><br /> 
		<input	type="button" value="登录" />
	</form>
	<div id="show"></div>
	<script>
			function Name() {

			var Name = document.getElementById("name").value;
			var name = /^[A-Z][0-9A-Za-z][a-zA-Z0-9]{8,14}$/; // 大写字母开头, 9-15位字符
			if (!name.test(Name)) {
				return false;
				$("#name").after("x");
			} else {
				return true;
			$("#name").after("");
			}
		}
		function Pwd() {
			var Pwd = document.getElementById("pwd").value;
			var pwd = /^[A-Z][A-Za-z0-9]\w{6,12}.{1,20}$/; //大写开头 数字字母符号混合 7-13位
			if (!pwd.test(Pwd)) {
				$("#pwd").after("x");
				return false;
			} else {
				$("#pwd").after("");
				return true;
			}
		}
	
		$(function() {
			$(":button").on("click", function() {
				$.ajax({
					type : "post",
					url : "/servlet/tese",
					data : {
						name : $("#name").val(),
						pwd : $("#pwd").val()
					},
					dataType : "text",
					success : function(data) {
						if (data == "ok") {
							window.location.href = "show.jsp";
						} else {
							alert("登录失败!");
							$("#pwd").val("");
							$("#name").focus().select();
						}
					}
				});
			});
		});
	</script>
</body>
</html>

servlet代码:

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 tese 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 {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		String name = "zhangsan";
		String pwd = "123456";
		
		String ajaxName = request.getParameter("name");
		String ajaxPwd = request.getParameter("pwd");
		System.out.println(ajaxName+":"+ajaxPwd);
		if(name.equals(ajaxName)&&pwd.equals(ajaxPwd)){
			out.print("ok");
		}else{
			out.print("登陆失败");
		}
		out.flush();
		out.close();
	}
}

最后:代码还有一点小问题 暂时还没解决 所以效果暂时没办法显示出来。

你可能感兴趣的:(使用ajax与正则表达式实现验证登录)