简体   繁体   English

如何使用jQuery验证JSP密码字段?

[英]how to validate jsp password fields using jQuery?

I would like to check (into a jsp page) whether the values of password1 & password2 are equal or not. 我想检查(进入jsp页面)password1和password2的值是否相等。 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 在这里,动作是一个struts2动作,它定义在struts.xml文件中

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: 和jQuery脚本代码:

$(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? 并提供用户再次输入密码?

You are looking for event.preventDefault() 您正在寻找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. 请注意将event参数传递到点击处理程序中。

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

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(); 您可以通过wnd.close();关闭窗口wnd.close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM