Question

I would like to check (into a jsp page) whether the values of password1 & password2 are equal or not. And if they are not equal, it will stay on the same page. Otherwise it will update. I could do all things but I am unable to make the form to stay on the same page if the passwords are not equal. the codes which i tried are like the following:

  • here, action is a struts2 action which is defined into struts.xml file

Form code:

<form id="form1" method="post"  action="saveUpdatePassword">
    Password:         <s:password name="password1" id="password1" />
    Re-type password: <s:password name="password2" id="password2" />
                      <input type="submit" id="sumbitButton" value="Update" />
</form>

and the jQuery script code:

$(function() {
    $("#sumbitButton").click(function() {
        if ($("#password1").val() != $("#password2").val()) {
            alert("Not equal");
        }           
    });
});

Can you tell me how to make the form staying on the same page if the password does not match? And provide users to put the password again?

Was it helpful?

Solution

You are looking for event.preventDefault()

$(function() {
    $("#sumbitButton").click(function(event) {
        if ($("#password1").val() != $("#password2").val()) {
            alert("Not equal");
            event.preventDefault();
        }           
    });
});

Note the event parameter being passed into the click handler.

http://api.jquery.com/event.preventdefault/

OTHER TIPS

If result is false then simply return false, which will prevent the form to post.

$(function() {
        $("#sumbitButton").click(function() {
            if ($("#password1").val() != $("#password2").val()) {
                alert("Not equal");
                return false;
            }           
        });
    });

EDIT: When password changed successfully

var wnd = window.open(blah,blah,blah . . . . .)

you can close the window as wnd.close();

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