佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > AJAX實現(xiàn)注冊驗證用戶名

AJAX實現(xiàn)注冊驗證用戶名

熱門標簽:電子圍欄 阿里云 Linux服務(wù)器 科大訊飛語音識別系統(tǒng) 團購網(wǎng)站 銀行業(yè)務(wù) Mysql連接數(shù)設(shè)置 服務(wù)器配置

本文實例為大家分享了AJAX實現(xiàn)注冊驗證用戶名的具體代碼,供大家參考,具體內(nèi)容如下

功能說明

當用戶在注冊頁面輸入用戶名并且鼠標焦點離開輸入框時,到數(shù)據(jù)表中去驗證該用戶名是否已經(jīng)存在,如果存在提示不可用,否則,提示可用。

接口

public interface UserDao {
 public User findName(String name);
}

接口實現(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頁面

%@ page contentType="text/html;charset=UTF-8" language="java" %>
html>
 head>
 title>AJAX實際操作注冊驗證用戶名/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>
 賬號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ù)庫如下:

運行結(jié)果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

您可能感興趣的文章:
  • jquery+ajax實現(xiàn)注冊實時驗證實例詳解
  • 用AJAX實現(xiàn)頁面登陸以及注冊用戶名驗證的簡單實例
  • PHP+Ajax異步通訊實現(xiàn)用戶名郵箱驗證是否已注冊( 2種方法實現(xiàn))
  • Ajax驗證用戶名或昵稱是否已被注冊
  • Asp.net下利用Jquery Ajax實現(xiàn)用戶注冊檢測(驗證用戶名是否存)
  • Ajax注冊用戶時實現(xiàn)表單驗證
  • ajax對注冊名進行驗證檢測是否存在于數(shù)據(jù)庫中
  • AJAX+JAVA用戶登陸注冊驗證的實現(xiàn)代碼
  • asp ajax注冊驗證之 防止用戶名輸入空格
  • 使用struts2+Ajax+jquery驗證用戶名是否已被注冊

標簽:萍鄉(xiāng) 棗莊 廣元 江蘇 大理 衡水 蚌埠 衢州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《AJAX實現(xiàn)注冊驗證用戶名》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    蓝山县| 木里| 奈曼旗| 合作市| 九龙县| 综艺| 公安县| 渭南市| 汾西县| 泊头市| 英德市| 成武县| 犍为县| 昌邑市| 扬中市| 确山县| 呼伦贝尔市| 巴林左旗| 敦煌市| 体育| 洪洞县| 涡阳县| 武清区| 房产| 奎屯市| 邵阳县| 苏尼特右旗| 临泉县| 汶上县| 仙桃市| 丘北县| 长乐市| 张家界市| 牙克石市| 大田县| 福海县| 甘南县| 杂多县| 沅陵县| 正蓝旗| 且末县|