0

I am trying to validate a form using jquery with our validation plugin. When submit the form its redirect to the action page without validating.

Also I Tried by put the isValidFormLogin function in the Index.jsp as well.

Index.jsp

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap & JavaScripts-->
<script src="js/jquery-3.4.1.min.js" type="text/javascript"></script>
<script src="Controllers/controllerMain.js" type="text/javascript"></script>
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap & JavaScripts-->
<title>JQuery</title>
</head>
<body>
<div class="container">
    <div class="card mx-auto" style="width: 30rem;">           
            <div class="card-body">
                <h5 class="card-title"></h5>
                <form id="formLogin" action="Items.jsp" method="post">
                    <div class="form-group">
                        <label for="username">Username</label>
                        <input type="email" class="form-control" name="username" id="username" aria-describedby="emailHelp" placeholder="Enter Username">
                    </div>
                    <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" name="password" id="password" placeholder="Password">
                    </div>
                    <button name="btnLogin" id="btnLogin" type="submit" class="btn btn-primary">Login</button>
                 </form>
            </div>
            <div class="card-footer">
                <a href="#">Forgot Password ?</a>
            </div>
            <div id="msgLogin">
                <% //out.println(MsgLogin); %>
            </div>
        </div>
</div>
</body>

contollerMain.js

//Login 
$(document).on("click","#btnLogin",function(){
    var result = isValidFormLogin();
    if(result=="true"){
        $("#formLogin").submit();
    }
    else{
        $("#MsgLogin").html(result);
    }

});

function isValidFormLogin(){
    if($.trim($("#username").val())==""){
        return "Enter Username";
    }
    if($.trim($("#password").val())==""){
        return "Enter Password";
    }
    return "true";
}

3 Answers 3

3

Add prevent default to stop default submit button functionality :

 $(document).on("click","#btnLogin",function(e){
        e.preventDefault();
        var result = isValidFormLogin();
        if(result=="true"){
            $("#formLogin").submit();
        }
        else{
            $("#MsgLogin").html(result);
        }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Now Its no redirect.. But in my .jsp file <% out.println(MsgLogin); %> this line shows an error.
2

You need to cancel the event submission first using the event object.

$(document).on("click","#btnLogin",function(event){
// cancel submission
event.preventDefault();
var result = isValidFormLogin();
if(result=="true"){
    $("#formLogin").submit();
}
else{
    $("#MsgLogin").html(result);
}
});

1 Comment

Now Its no redirect.. But in my .jsp file <% out.println(MsgLogin); %> this line shows an error.
1

Submit button has default form submission functionality. You need to prevent before submitting the form.

$(document).on("click","#btnLogin",function(e){
e.preventDefault();
//your code
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.