• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating custom validators for client side validation in Javascript

 
Cam Cres
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need to show your configuration and the JSP of the form, at least the field you're trying to validate.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic