Question

How can i show alert box for validation in login form?

I got below output but i want alert box?

enter image description here

This is my index page: index.jsp

 <html:form styleId="validate" action="/account_login" styleClass="account" method="post">
                        <div class="content controls single_advance">
                            <!-- <div class="form-row user-change-row">-->
                            <div class=form-row>
                                <div class=col-sm-3></div>
                                <div class=col-sm-4>
                                    <bean:write name="account_bean" property="error" filter="false"/>
                                </div>

                            </div>
                            <div class="form-row">
                                <div class="col-md-12">
                                    <div class=input-group>
                                        <div class=input-group-addon> <span class=icon-user></span> </div>
                                        <!--<input type=text class=form-control placeholder="Account Name"/>-->
                                        <html:text property="name" styleClass="validate[required] form-control" styleId="Account_Name"/>
                                    </div>
                                </div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-12">
                                    <div class=input-group>
                                        <div class=input-group-addon> <span class=icon-key></span> </div>
                                        <!--<input type=password class=form-control placeholder="Password"/>-->
                                        <html:password property="password" styleClass="validate[required] form-control" styleId="Password"/>
                                    </div>
                                </div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-6 col-md-offset-3">
                                    <input class="btn btn-default btn-block btn-clean btn-success" value="Log In" type="submit">
                                </div>
                            </div>
                            <!--<div class=form-row>
                              <div class="col-md-12"> <a href=# class="btn btn-link btn-block">Forgot your password?</a> </div>
                            </div>-->
                            <div class=form-row>
                                <div class="col-md-12 tac"><strong>OR USE </strong></div>
                            </div>
                            <div class=form-row>
                                <div class="col-md-12"> <a class="btn btn-primary btn-block imeino"><span class="icon-mobile-phone"></span> &nbsp; IMEI NO LOGIN</a> </div>
                            </div>
                        </div>
                    </html:form>

This is my struts action class: account_actoin.java

 if (account_name.equals(acc_name) && account_password.equals(acc_password)) {

        HttpSession http_session = request.getSession(true);
        http_session.setAttribute("login_name", acc_name);


        ArrayList<String> name_list = new ArrayList<String>();

        Query enc_password = session.createQuery("from account_dao");
        List list = enc_password.list();
        for (Iterator iterator = list.iterator(); iterator.hasNext();) {
            account_dao account = (account_dao) iterator.next();
            System.out.println("Id : " + account.getId());
            System.out.println("Account Name : " + account.getName());
            System.out.println("Account Password : " + account.getPassword());
            int i = 0;
            name_list.add(i,account.getName());
            i++;
        }
        request.setAttribute("namelist", name_list);
        dealer_bean Bean = new dealer_bean();
        Bean.setCustomerList(name_list);
        System.out.println("List : "+name_list);


        return mapping.findForward(SUCCESS);
    } else {
        fromBean.setError("<span style='color:red'><h6>Login failed.</h6></span>");
        return mapping.findForward(FAILURE);
    }

How can I show error (or) success message in alert box?

Was it helpful?

Solution

Append after the html:form close tag the next code and try:

<logic:present name="account_bean" property="error">
<div>
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.9.1.js"></script>
    <script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>
        // When DOM is ready
        $(function() {
            $("#errorDialog").dialog({
                title: "Login",
                autoOpen: false,
                resizable: false,
                show: "clip",
                hide: "clip",
                modal: true,
                dialogClass: "ui-state-active"
            });
        });
        // When Window is ready
        $(window).load(function() {
            $("#errorDialog").dialog("open");
        });
    </script>
    <div id="errorDialog">
        <bean:write name="account_bean" property="error" filter="false"/>
    </div>
</div>
</logic:present>

OTHER TIPS

Simply try using struts logic tags for your purpose. Here i used jQuery ready() function,

<logic:present name="account_bean" property="error">
<script>
    $(document).ready(function() {
        alert('Login failed.');
    });
</script>
</logic:present>

Hope this helps..

I haven't tried it and I am not even sure if this works. It was too long to be written as a comment.

Create a field in your form bean as follows:

public class MyForm{
    String isUserValid;

    //its getters and setters
}

And then in your account_actoin.java

if (account_name.equals(acc_name) && account_password.equals(acc_password)) {
    //rest of your code

    formBean.setIsUserValid("Y")//------------Add this
    return mapping.findForward(SUCCESS);
else {
        formBean.setIsUserValid("N")//--------And this
        return mapping.findForward(FAILURE);
    }

In your jsp file add a hidden input and the following script as follows:

<head>
<script type="text/javascript">
    $(document).ready(function(){
         var isLoggedIn = $("#isUserValid").val();
         if(isLoggedIn == "N"){
               alert("Log in failed");
         }
    });
</script>
</head>
<body>
<input type="hidden" id="isUserValid" name="isUserValid" />

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top