当前位置:编程学习 > JAVA >>

关于注册时验证用户名是否存在的问题,在线等

<%@ 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>注册页面</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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

<script type="text/javascript">

function executeAjax(){
var url="${pageContext.request.contextPath}/RegisterAction!register.action?cardid="+$("#cardid").val();
$.get(url,null,function(str){
message(str);
});
}

function message(str){
var sp = document.getElementById("cardidMsg");
if(str == "0"){
sp.style.color="red";
var cardidMsg="卡号已存在。";
}else if(str == "1"){
sp.style.color="green";
var cardidMsg="卡号正确。";
}
sp.innerHTML = cardidMsg;
}
        
        function checkcardid(){
var div = document.getElementById("div1");
            div.innerHTML = "";
            var cardid = document.form4.cardid.value;
            if (cardid == "") {
                div.innerHTML = "卡号不能为空!";
                document.form4.cardid.focus();
                return false;
            }
            if (cardid.length < 3 || cardid.length > 16) {
                div.innerHTML = "卡号输入的长度3-16个字符!";
                document.form4.cardid.select();
                return false;
            }
            var charcardid1 = cardid.toLowerCase();
            for (var i = 0; i < cardid.length; i++) {
                var charcardid = charcardid1.charAt(i);
                if (!(charcardid >= 0 && charcardid <= 9)) {
                    div.innerHTML = "卡号只能输入数字!";
                    document.form4.cardid.select();
                    return false;
                };
            }
            
            return true;
            
        }
        
        function checkrecardid(){
            var div = document.getElementById("div2");
            div.innerHTML = "";
            var cardid = document.form4.cardid.value;
            var recardid = document.form4.trancardid.value;
            if (recardid == "") {
                div.innerHTML = "不能为空!";
                document.form4.trancardid.focus();
                return false;
            }
            if (cardid != recardid) {
                div.innerHTML = "与输入卡号不一致";
                document.form4.trancardid.select();
                return false;
            }
            return true;
}
        
        function checkpassword(){
            var div = document.getElementById("div3");
            div.innerHTML = "";
            var password = document.form4.password.value;
            if (password == "") {
                div.innerHTML = "密码不能为空!";
                document.form4.password.focus();
                return false;
            }
            if (password.length < 3 || password.length > 12) {
div.innerHTML = "密码长度3-12位";
                document.form4.password.select();
                return false;
            }
            return true;
        }
        
        function checkrepassword(){
            var div = document.getElementById("div4");
            div.innerHTML = "";
            var password = document.form4.password.value;
            var repass = document.form4.password2.value;
            if (repass == "") {
                div.innerHTML = "验证密码不能为空!";
                document.form4.password2.focus();
                return false;
            }
            if (password != repass) {
                div.innerHTML = "验证密码和输入密码不一致";
                document.form4.password2.select();
                return false;
            }
            return true;
}
        
        function checkname(){
var div = document.getElementById("div5");
            div.innerHTML = "";
            var name1 = document.form4.username.value;
            if (name1 == "") {
                div.innerHTML = "用户名不能为空!";
                document.form4.username.focus();
                return false;
            }
            
            return true;
            
        }
        
        function check(){
            if (checkcardid() && checkrecardid() && checkpassword() && checkrepassword() && checkname()) {
                return true;
            }
            else {
                return false;
            }
        }
</script>

  </head>
  
  <body onload="document.forms[0].elements[0].focus()">
    <form action="<%=request.getContextPath()%>/RegisterAction!register.action" name="form4" method="post" onsubmit="return check()">
     <table align="center" style="margin-left: 460px;">
     <tr>
     <td>
     <tr>
     <th>卡号:</th>
     <td>
     <input id="cardid" type="text" name="user.cardid" onblur="check()" />
     <div id="div1" style="display:inline"></div><span id="cardidMsg"></span>
     </td>
     </tr>
     <tr>
     <th>再次输入卡号:</th>
     <td>
     <input id="trancardid" type="text" name="tran.cardid" onblur="check()" />
     <div id="div2" style="display:inline"></div>
     </td>
     </tr>
     <tr>
     <th>用户密码:</th>
     <td>
     <input id="password" type="password" name="user.password" onblur="check()">
                         <div id="div3" style="display:inline"></div>
     </td>
     </tr>
     <tr>
     <th>验证密码:</th>
     <td>
     <input id="password2" type="password" name="password2" onblur="check()">
                         <div id="div4" style="display:inline"></div>
     </td>
     </tr>
     <tr>
     <th>用户名:</th>
     <td>
     <input id="username" type="text" name="user.username" onblur="check()" />
     <div id="div5" style="display:inline"></div>
     </td>
     </tr>
     <table align="center" width="250">
     <tr>
<td colspan="" class="">
<input type="submit" value="注册" />
<input type="reset" value="重置" onclick="if (confirm('确定要重置吗?')){document.form4.cardid.focus();return true;}  else return false;" />
<input type="button" value="返回" onclick="window.location.href='login.jsp'" />
</td>
</tr>
</table>
     </td>
     </tr>
     </table>
    </form>
  </body>
</html>


当我在卡号处失去焦点时只验证了checkname的内容,而上面的验证卡号是否存在不好使,那位大侠帮忙看看呀,我数据库用的是mysql的,项目是单独struts2框架写的。 --------------------编程问答-------------------- 大哥大姐们帮帮忙呀~ --------------------编程问答-------------------- onblur="check()",你每个输入框都有这个,当你输入第一个卡号框的时候,剩下的都是空的,效率太低了 --------------------编程问答--------------------
引用 2 楼  的回复:
onblur="check()",你每个输入框都有这个,当你输入第一个卡号框的时候,剩下的都是空的,效率太低了



额,这个不是重点,重点是验证注册卡号是否重复那段script没好使,帮忙解决一下呀。 --------------------编程问答-------------------- 使用AJAX简单点,试试吧。
--------------------编程问答--------------------


function executeAjax(){
            var url="${pageContext.request.contextPath}/RegisterAction!register.action?cardid="+$("#cardid").val();
            $.get(url,null,function(str){
                message(str);
            });
        }
        
        function message(str){
            var sp = document.getElementById("cardidMsg");
            var cardidMsg="";
            if(str == "0"){
                sp.style.color="red";
                cardidMsg="卡号已存在。";
            }else if(str == "1"){
                sp.style.color="green";
                cardidMsg="卡号正确。";
            }
            sp.innerHTML = cardidMsg;
        }




cardidMsg定义成局部变量了 --------------------编程问答-------------------- 遮掩验证太复杂了,你用什么框架啊!
补充:Java ,  Web 开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,