简体   繁体   English

JQuery没有验证struts2表单

[英]JQuery not validating struts2 form

I am using struts2 framework for my application. 我正在为我的应用程序使用struts2框架。 I am also using jquery validation for validating form input at client side. 我也使用jquery验证来验证客户端的表单输入。 however they both don't go with one another quite well. 但是他们俩都没有很好地相处。

I have the UserBean Class which I want it to be there. 我有UserBean类,我希望它在那里。

I m using the following jquery & struts form.. which does not validate the form 我使用以下jquery&struts表单..它不验证表单

    <script type="text/javascript">
        $(document).ready(function(){
            var validator = $("#myform").validate({
                rules: {
                    username: "required",
                    password: "required"
                },
                messages: {
                    username: "Please enter your username",
                    password: "Please enter your password"
                }
            });
        });
    </script>

 <s:form method="POST" action="doLogin" name="myform" id="myform" cssClass="form" theme="simple">
            <s:fielderror theme="simple" >

                    <label class="control-label" for="username">User Name </label>

                        <s:textfield name="userBean.userName" label="USER NAME"/>
                        <s:param>userBean.userName</s:param>



                    <label class="control-label" for="username">Password  </label>


                        <s:password name="userBean.password" label="PASSWORD" />
                        <s:param>userBean.password</s:param>

                <s:submit name="Submit" value="Submit"/>
            </s:fielderror>
        </s:form>

You input names are userBean.userName and userBean.password so your script declaration must match those names in order to work: 您输入的名称是userBean.userNameuserBean.password因此您的脚本声明必须与这些名称匹配才能工作:

<script type="text/javascript">
    $(document).ready(function(){
        var validator = $("#myform").validate({
            rules: {
                "userBean.userName": "required",
                "userBean.password": "required"
            },
            messages: {
                "userBean.userName": "Please enter your username",
                "userBean.password": "Please enter your password"
            }
        });
    });
</script>

And as per jQuery Validation on field(s) with a prefix in the name property , you need to put the name in quotes because it contains a . 并且根据jQuery验证在name属性中带有前缀的字段 ,您需要将名称放在引号中,因为它包含一个. in the name. 在名字里。

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

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