File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Javascript and Struts validator

 
Yash Duddela
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using struts validator in my project and requirement is that javascript client side validation meesages must be displayed on the page unlike pop up(alert window).

Is there a work around for this with ActionErrors?

Please guide.

Thanks,
Summu
 
Shankar Narayana
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cudn't get you what you need, can you be more clear.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, Summu!

If I understand your question correctly, you'll want to write your own JavaScript validation scripts. To get an idea for doing this and for how to integrate them with the framework, I'd recommend taking a look at the contents of your validator-rules.xml file (which you might have named something else).

I'm not sure how to write a JavaScript script to change page text, but I'm sure folks over in our HTML and JavaScript forum have some ideas.
[ July 28, 2004: Message edited by: Dirk Schreckmann ]
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a work around for this with ActionErrors?


What does it mean ???

please explain more detail.
 
Nicola Guy
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If after you find out validation has failed you use
errors.add("fieldname", new ActionError("key.fromresourcefile");

Then add <html:errors> to your jsp.

think thats what you want
 
Yash Duddela
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question was for client side validations, for example,

For a numeric field (phone number) I want to validate it so that no characters are entered.

So, with validator it would throw me a pop up saying "Phone number is invalid". But I want to write this message on to the page, blocking pop up.

Thanks for the response.

Summu
 
Nicola Guy
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I said will work for that, you include the errors.add..... line in the validate method of your action form and your errors will be displayed ont he page where you put <html:errors/>
 
Mike Firkser
Ranch Hand
Posts: 249
Java Oracle PHP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to display errors on the page using JavaScript, you may want to try having an empty <p> tag with a name, and then use the innerText or innerHTLM properties to fill it.



You may also want to give the <p> tag a name attribute, and also use getElementByName(), in case the browser doesn't like getElementById.
 
Yash Duddela
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mike I implemeted this solution I have to yet work using on ActionErrors.
 
John Carnell
Author
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Summu,

The ActionErrors class is used to hold errors found during a "server-side" validation. If you want to use Javascript then you can not access the ActionErrors object.

You can either put the JavaScript directly in the JSP page or you can use the validator framework. All of the standard validation rules in the Struts/validator plugin support a client-side version of their rules.

In addition, you can extend the validator framework to include JavaScript-based validation rules. You can then tie the validation to your struts form and have Struts automatically include the Javascript validation rule.

Thanks,
John
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
VAlidation in Client Side (JavaScript) with Validation Framework

1. Config validation.xml

example



2. In JSP Page

 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And ApplicationResource.properties file

 
Ratnakar Parakala V
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You JSP might be having the following line:

<html:javascript formName="your_FormName" />

This code generates necessary javascript validations using Validation-Rules.xml. If you remove the above code, you won't get any JS pop ups. In this cases, Validator framework will fire the serverside validation using same validation-rules.xml and validation.xml

Hope this helps.

Ratnakar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic