首页 > java如何让注册的用户名不重复,在当前页面就可以判断,并抛出提示?

java如何让注册的用户名不重复,在当前页面就可以判断,并抛出提示?

差的资。料说是用ajax做?可是我不会ajax,ajax实在不会写了。。。

html部分

<p>
        <label>编号:</label> <input name="custom_number" type="text" size="30" id="custom_number"
                    value="<%=item.getCustom_number() == null ? "" : item.getCustom_number()%>"
                    maxlength="50" />
</p>

javascript部分

<script>
$('#custom_number').change(function(){    
                $.ajax({    
        url: '<%=basePath%>admin/custom/repeat.html?custom_number=' + $('#custom_number').val(),
        dataType: 'post',
        
        });
</script>

后台数据库代码

 @RequestMapping(value = "/repeat")
     public void repeat(HttpServletRequest request, HttpServletResponse response, String custom_number) throws SQLException, IOException{
         ConnectionSource connectionSource = DBUtil.getConnectionSource();
         
         Dao<zqbp_custom, String> dao = DaoManager.createDao(connectionSource,
                 zqbp_custom.class);
         
         QueryBuilder<zqbp_custom, String> where = dao.queryBuilder();
         List<zqbp_custom> list = where.where().eq("custom_number", custom_number).query();
         if(list.size()==0){
             
         }else {
             response.getWriter().write("编号重复,请重新输入");
              response.getWriter().close();
        }
     }

jquery validation找个这个来用
当然自己做也没什么难的 最多做的丑一点


一个最简单的实现。

在线预览: https://code.levey.cn/ajax_check_name/

已注册用户名 leveyswnuv


Code:

index.html

<html>
<head>
    <meta charset="UTF-8">
    <title>Check UserName</title>
    <script src="js/jquery.min.js"></script>
    <script src="check.js"></script>
</head>
<body>
<label for="username">用户名</label><input type="text" size="16" name="username" id="username"/><font color="red">*</font>
<span id="tips"></span>
</body>
</html>

这里就是ajax了

check.js

$(document).ready(function(){
    checkUserName();
});
//验证用户名是否存在
function checkUserName(){
    $("#username").blur(function(){
        var username = $(this).val();
        //此处替换你自己的jsp路径,jsp返回值:存在输出1,不存在输出0
        var changeUrl = "check.php?username=" + username;
        $.get(changeUrl,function(str){
            if(str == '1'){
                $("#tips").html("<font color=\"red\">您输入的用户名存在!请重新输入!</font>");
            }else{
                $("#tips").html("<font color=\"green\">恭喜您,可以注册!</font>");
            }
        });
        return false;
    })
}

这个页面用jsp实现就行了
check.php

<?php
session_start();
header("Content-type:text/html;charset=utf-8");
if (isset($_GET['username'])) {
    $username = $_GET['username'];
    $check1 = "levey";
    $check2 = "swnuv";
    if ($username == $check1 || $username == $check2) {
        echo 1; 
        exit;
    }else{
        echo 0;
        exit;
    }
}

1.你后台肯定得检索数据库。
2.前台直接判断就得发送请求。
3.你不想跳转,那只能是ajax发送请求。

你上网google一个例子,我就不当搬运工了,ajax就是个js发送http请求。

另外题主你说java?还是javascript?

【热门文章】
【热门文章】