码迷,mamicode.com
首页 > Web开发 > 详细

ajax验证用户名是否存在

时间:2014-12-18 20:22:46      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   color   os   sp   java   

js代码

function validatorloginName(){  
    var loginName=document.getElementById("suser").value; 
    var reg=new RegExp("^[a-zA-Z]{1}[a-zA-Z0-9]{5,11}$");
    if(loginName == "")  
    {  $("input[name=‘sname‘]").next().removeClass("ok");
       $("input[name=‘sname‘]").next().html("用户名不能为空!");
       $("input[name=‘sname‘]").next().addClass("err");  
       return;  
    }  
    else if(!reg.exec(loginName)){
        $("input[name=‘sname‘]").next().removeClass("ok");
        $("input[name=‘sname‘]").next().html("用户名格式错误,首位必须是字母长度不能小于六位!");
        $("input[name=‘sname‘]").next().addClass("err");
        return false;
    }else{
        $("input[name=‘sname‘]").next().html("");
         $.ajax({  
               type: "POST",      
                url: "../servlet/ValidateName",      
                 data: "loginName="+loginName,   
                success: function(data){  
               if(data=="true"){ 
                   $("input[name=‘sname‘]").next().html("");
                   $("input[name=‘sname‘]").next().addClass("ok");
                   return true;
                
               }else{ 
                   $("input[name=‘sname‘]").next().removeClass("ok");
                   $("input[name=‘sname‘]").next().html("抱歉!用户名已存在!");
                   $("input[name=‘sname‘]").next().addClass("err");
                   return false;
               }   
               }            
               });     
        
    }
}

web.xml配置

  <servlet>
  <servlet-name>ValidateName</servlet-name>
    <servlet-class>Util.ValidateName</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ValidateName</servlet-name>
    <url-pattern>/servlet/ValidateName</url-pattern>
  </servlet-mapping>

java代码

package Util;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;


public class ValidateName extends HttpServlet {
    public ValidateName(){
        super();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        String loginName=request.getParameter("loginName").toString();
    
        try {
            Context ctx = new InitialContext();
            DataSource    ds = (DataSource)ctx.lookup("java:/comp/env/jdbc/oracle");
            Connection conn = ds.getConnection();
            PreparedStatement pst = conn.prepareStatement("select * from huiyuan where suser=‘"+loginName+"‘ ");
            ResultSet res=pst.executeQuery();
            boolean flog=res.next();
            if(!flog){
            response.getWriter().write("true");//此值jquery可以接收到  
            }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        }

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

        this.doGet(request, response); 
        
    }

}

 

ajax验证用户名是否存在

标签:style   blog   http   ar   io   color   os   sp   java   

原文地址:http://www.cnblogs.com/Dreamlu/p/4172456.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!