Reputation: 2404
I am using struts2 framework for my application. I am also using jquery validation for validating form input at client side. however they both don't go with one another quite well.
I have the UserBean Class which I want it to be there.
I m using the following jquery & struts form.. which does not validate the form
<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>
Upvotes: 3
Views: 3167
Reputation: 8686
You input names are userBean.userName
and userBean.password
so your script declaration must match those names in order to work:
<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 .
in the name.
Upvotes: 2