开发者社区> 问答> 正文

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

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();
        }
     }

展开
收起
蛮大人123 2016-03-10 15:38:02 3746 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    一个最简单的实现。
    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实现就行了

    <?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;
        }
    }
    2019-07-17 18:57:47
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载