AJAX實(shí)現(xiàn)注冊(cè)驗(yàn)證用戶名
本文實(shí)例為大家分享了AJAX實(shí)現(xiàn)注冊(cè)驗(yàn)證用戶名的具體代碼,供大家參考,具體內(nèi)容如下
功能說(shuō)明
當(dāng)用戶在注冊(cè)頁(yè)面輸入用戶名并且鼠標(biāo)焦點(diǎn)離開(kāi)輸入框時(shí),到數(shù)據(jù)表中去驗(yàn)證該用戶名是否已經(jīng)存在,如果存在提示不可用,否則,提示可用。
接口
public interface UserDao { public User findName(String name); }
接口實(shí)現(xiàn)類
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class UserDaoImpl implements UserDao { @Override public User findName(String name) { User user =null; Connection conn = DBHelper.getConn(); String sql = "select * from user where name=?"; try { PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1,name); ResultSet rs = ps.executeQuery(); if (rs.next()){ user = new User(); user.setId(rs.getInt(1)); user.setName(rs.getString(2)); user.setPassword(rs.getString(3)); } } catch (SQLException e) { e.printStackTrace(); } return user; } }
servlet
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet("/findName") public class FindNameServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String name = request.getParameter("name"); UserDao userDao = new UserDaoImpl(); User name1 = userDao.findName(name); if (name1!=null){ response.getWriter().write("1"); }else { response.getWriter().write("2"); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
JSP頁(yè)面
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>AJAX實(shí)際操作注冊(cè)驗(yàn)證用戶名</title> <script src="js/jquery-1.8.3.js"></script> </head> <body> <form action="#" method="post"> <script type="text/javascript"> $(function () { $("[name=userName]").blur(function () { $.ajax({ type:"get", url:"findName?name="+$("[name=userName]").val(), dataType:"text", success:function (data) { //alert(data); if (data=="1"){ $("#show").html("用戶已存在!??!") }else { $("#show").html("用戶名可用") } } }) }) }); </script> 賬號(hào)<input type="text" name="userName"><span id="show"></span></br> 密碼<input type="password" name="password"></br> <input type="submit" value="提交"> </form> </body> </html>
數(shù)據(jù)庫(kù)如下:
運(yùn)行結(jié)果如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jquery+ajax實(shí)現(xiàn)注冊(cè)實(shí)時(shí)驗(yàn)證實(shí)例詳解
- 用AJAX實(shí)現(xiàn)頁(yè)面登陸以及注冊(cè)用戶名驗(yàn)證的簡(jiǎn)單實(shí)例
- PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
- Ajax驗(yàn)證用戶名或昵稱是否已被注冊(cè)
- Asp.net下利用Jquery Ajax實(shí)現(xiàn)用戶注冊(cè)檢測(cè)(驗(yàn)證用戶名是否存)
- Ajax注冊(cè)用戶時(shí)實(shí)現(xiàn)表單驗(yàn)證
- ajax對(duì)注冊(cè)名進(jìn)行驗(yàn)證檢測(cè)是否存在于數(shù)據(jù)庫(kù)中
- AJAX+JAVA用戶登陸注冊(cè)驗(yàn)證的實(shí)現(xiàn)代碼
- asp ajax注冊(cè)驗(yàn)證之 防止用戶名輸入空格
- 使用struts2+Ajax+jquery驗(yàn)證用戶名是否已被注冊(cè)
相關(guān)文章
Spring MVC+ajax進(jìn)行信息驗(yàn)證的方法
AJAX 不是新的編程語(yǔ)言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。這篇文章主要介紹了Spring MVC+ajax進(jìn)行信息驗(yàn)證,需要的朋友可以參考下2019-09-09jQuery Ajax使用心得詳細(xì)整理及注意事項(xiàng)
jQuery Ajax相關(guān)文章想必大家在網(wǎng)上面已經(jīng)看到了很多,本文主要目的是整理jQuery Ajax相關(guān)內(nèi)容,感興趣的朋友可以了解下,或許本文對(duì)你有所幫助2013-02-02Ajax上傳實(shí)現(xiàn)根據(jù)服務(wù)器端返回?cái)?shù)據(jù)進(jìn)行js處理的方法
這篇文章主要介紹了Ajax上傳實(shí)現(xiàn)根據(jù)服務(wù)器端返回?cái)?shù)據(jù)進(jìn)行js處理的方法,實(shí)例分析了Ajax請(qǐng)求及java處理并返回服務(wù)器端數(shù)據(jù)請(qǐng)求的相關(guān)技巧2015-07-07利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算
這篇文章主要為大家詳細(xì)介紹了利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03解決Ajax方式上傳文件報(bào)錯(cuò)"Uncaught TypeError: Illegal invocation"
這篇文章主要介紹了Ajax方式上傳文件報(bào)錯(cuò)"Uncaught TypeError: Illegal invocation",非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06