aspose file tools*
The moose likes Struts and the fly likes Creating custom validators for client side validation in Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Creating custom validators for client side validation in Javascript" Watch "Creating custom validators for client side validation in Javascript" New topic
Author

Creating custom validators for client side validation in Javascript

Cam Cres
Greenhorn

Joined: Apr 16, 2010
Posts: 2
Hello guys:
I'm trying to extend the default validators that come with the Struts Validation framework. This is the entry in my validation-rules.xml for my new custom made validator

<!-- My OWN defined validators -->
<validator name="positiveInt"
classname="com.aexp.webproj.validators.MyOwnValidator"
method="validatePositive"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.positive">

<javascript><![CDATA[
function validatePositive(form)
{
return true;
}]]>
</javascript>

</validator>

Whenever I do server side validation it works. I can properly see a message stating that the field in question is not a positive integer. I did that in MyOwnValidator class. My problem is when I try to do client side validation. I tried to write a javascript funcion with the same logic that the method that does the server side validation and I passed it within the <javascript> element as in the example above. But I could not get any message or pop up I as got with the default validators. Then I tried returning false or true to experiment results, and nothing ever happened.
What I'm missing. Can someone explain how javascript validation works? Do I have to provide my own javascript code for every custom validator or there is a workaround to that?
I tried viewing the source of the final rendered page and I still could see that my validatePositive(form) exists. Also the message that is supposed to be displayed is there. Then why is nothing happening?

Thanks
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You'll need to show your configuration and the JSP of the form, at least the field you're trying to validate.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Creating custom validators for client side validation in Javascript