使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟
我們來(lái)分一下步驟吧:
1.html代碼,頁(yè)面先寫(xiě)出來(lái);
2.正則表達(dá)式驗(yàn)證輸入的用戶(hù)名密碼是否正確,失去焦點(diǎn)驗(yàn)證
3.ajax異步提交
4.servlet這是后臺(tái)處理代碼獲取數(shù)據(jù)并對(duì)比響應(yīng),然后跳轉(zhuǎn)成功頁(yè)面
效果圖:
結(jié)構(gòu):
代碼如下:
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <script type="text/javascript" src="js/jquery.js"></script> <style type="text/css"> table { width: 360px; height: 45px: text-align: center; margin-top: 120px; border-collapse: collapse; } input { width: 280px; height: 30px; } </style> </head> <body> <form action="#" method="post"> <center> <table align="center" border="1"> <tr> <td>用戶(hù)名:</td> <td><input type="text" name="name" id="username" onblur="verifyname()" /></td> </tr> <tr> <td>密碼:</td> <td><input type="text" name="pwd" id="mypwd" onblur="verifypwd()" /></td> </tr> <tr> <td colspan="3" align="center" height="36px"><input type="button" style="width: 8rem;height:27px" value="提交登錄驗(yàn)證" /></td> </tr> </table> </center> </form> <script type="text/javascript"> function verifyname() { //用戶(hù)名校驗(yàn) var verifyname = document.getelementbyid("username").value; var name = /^[a-z][0-9a-za-z_][a-za-z0-9_]{5,19}$/; // 大寫(xiě)字母開(kāi)頭 6-20位字符(不允許有符號(hào)但是允許有_) if (!name.test(verifyname)) { //$("#username").after("大寫(xiě)字母開(kāi)頭6-20位字符(不允許有符號(hào)但是允許有_)"); $("#username").css("border-color", "red"); return false; } else { return true; } } function verifypwd() { //密碼 var verifypwd = document.getelementbyid("mypwd").value; var pwd = /^[a-z][a-za-z0-9]\w{7,14}.{1,20}$/; //大寫(xiě)開(kāi)頭 數(shù)字字母符號(hào)混合 8-15位 if (!pwd.test(verifypwd)) { $("#username").css("border-color", "red"); return false; } else { return true; } } $(function() { $(":button").on("click", function() { $.ajax({ type : "post", url : "ajaxservlet", data : { name : $("#username").val(), pwd : $("#mypwd").val() }, datatype : "text", success : function(data) { if (data == "ok") { window.location.href = "show.jsp"; } else { alert("登錄失??!"); $("#mypwd").val(""); $("#username").focus().select(); } } }); }); }); </script> </body> </html>
servlet代碼:
package com.chaz.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 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 { response.setcontenttype("text/html;charset=utf-8"); request.setcharacterencoding("utf-8"); printwriter out = response.getwriter(); string name = "zhangsan"; string pwd = "zhang123456"; 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("error"); } out.flush(); out.close(); } }
web.xml:
<?xml version="1.0" encoding="utf-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <description>this is the description of my j2ee component</description> <display-name>this is the display name of my j2ee component</display-name> <servlet-name>ajaxservlet</servlet-name> <servlet-class>com.chaz.servlet.ajaxservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ajaxservlet</servlet-name> <url-pattern>/ajaxservlet</url-pattern> </servlet-mapping> </web-app>
跳轉(zhuǎn)成功頁(yè)面就這個(gè)😄:
<body> 登錄成功!</body>
總結(jié)
以上所述是小編給大家介紹的使用ajax(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)碩編程網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!