khun_1 0 Newbie Poster

I want to check if the username existed in my database against the input of the user after the button is clicked.

If the username existed, it will show "Username Not Avaliable" in the span and the username textbox will be highlighted in red. I tried to use ajax for the function and bootstrap "has-error" to achieve my highlight the textbox in red when the username is found in my database but I still don't understand how to piece everything together to achieve my desired output as I only begin to learn ajax and javascript recently.

Please help. thanks.


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" ""> <html> <head> <title>E-Commence J2EE</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet"
    href=""> <script
    src=""></script> <script
    src=""></script> <script type="text/javascript" src="js/navbarscroll.js"></script> <script type="text/javascript" src="assets/js/bootstrap.js"></script> <script src="js/alert-modal.js"></script> <script>
function checkAvail() {
    var xhttp;
    var v=document.myForm.username.value;  
    var url="checkAvail.jsp?val="+v;  
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var existed = this.responseText;
            if(existed === 'true') {
                    highlight: function(element) {
                        var id_attr = "#" + $(element).attr("username");
    };"POST",url, true);
</script> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <form role="form" onsubmit="return validateForm()" name="myForm"
        action="RegistrationCheck" method="POST"> <div class="form-group row"> <label for="regUsername " class="col-md-2">Username</label> <div class="col-md-10"> <input type="text" class="form-control" id="username"
                    name="username" onblur="checkAvail()" /> <span id="wes"></span> </div> <div style="text-align: center;"> <button a id="checkAvailBtn" href="javascript:;" type="submit"
                    class="btn btn-default">check Avalibility</button> </div> </div> </form> </body> </html>


<%@ page import="java.sql.*"%> <%
    String s = request.getParameter("val");

    try {
        Connection con = DriverManager
                        "root", "1234");
        PreparedStatement ps = con
                .prepareStatement("SELECT uname FROM member WHERE uname= ?");
        ps.setString(1, s);
        ResultSet rs = ps.executeQuery();
        while (rs.first()) {
    } catch (Exception e) {
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.