File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Javascript and Struts validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Javascript and Struts validator" Watch "Javascript and Struts validator" New topic
Author

Javascript and Struts validator

Yash Duddela
Greenhorn

Joined: Jul 10, 2003
Posts: 4
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

Joined: Jan 08, 2003
Posts: 134
Cudn't get you what you need, can you be more clear.


"Failure is not when you fall down; its only when you fail to get up again."
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
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 ]

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Is there a work around for this with ActionErrors?


What does it mean ???

please explain more detail.


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Nicola Guy
Ranch Hand

Joined: Jun 23, 2004
Posts: 91
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

Joined: Jul 10, 2003
Posts: 4
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

Joined: Jun 23, 2004
Posts: 91
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

Joined: Oct 21, 2003
Posts: 247

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.


Mike Firkser
Rutgers '84
Yash Duddela
Greenhorn

Joined: Jul 10, 2003
Posts: 4
Thank you Mike I implemeted this solution I have to yet work using on ActionErrors.
John Carnell
Author
Ranch Hand

Joined: Sep 27, 2002
Posts: 71
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


John Carnell<br />Principal Architect<br /> <br />Netchange, LLC<br />1161 HillCrest Heights<br />Green Bay, WI 54313<br /> <br />john.carnell@netchange.us<br /> <br /> <br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/159059228X/ref=jranch-20" target="_blank" rel="nofollow">Pro Jakarta Struts, Second Edition</a>
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
VAlidation in Client Side (JavaScript) with Validation Framework

1. Config validation.xml

example



2. In JSP Page

somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
And ApplicationResource.properties file

Ratnakar Parakala V
Greenhorn

Joined: Aug 05, 2004
Posts: 2
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Javascript and Struts validator