Forums Register Login
Clarify on using struts validator and javascript together
Hi Iam using struts 1.2.8. I have few questions regarding usage of javascript in struts. Pls Clarify.

1) In struts can i use javascript in the jsp pages and still use the validator framework to do some validations in the validate method in the form class?

2)How do i refer to the form name in the javascript code? Trying to use "name" attribute in <html:form> throws error.

3)<html:text property="userName">
In the above can the property attribue of the element be treated as the name of the element and used in javascript??

Thanks for ur time in advance!
Hi Deepthi...
I don't know too much about Struts but since I'm also working on the Plugin validation, I think that writing your own JS code will interfere with the dynamic generation of the JS from the Plugin. If you would have noticed, you need to make a call using a function name in the html:form action tag. The Plugin takes this name and puts all the generated JS inside this function and makes all its calls from this. Suppose you write your own JS code, you need to call it somewhere, which is where I think the overlap occurs. Suppose you have set the Plugin validator to run 'onsubmit', maybe you can call your own JS code on someother event, but never 'onsubmit' as the Plugin's code goes there. I suggest you just try this out, but its not gonna work. You could do this and do a View Source after deploying your JSP, but I'm pretty much sure that the overlap is gonna happen. Hope this helps out. As to the thing about referring to the form name, in the JS code, pass the 'this' object as a parameter to the JS function, and catch it as a form object in the function definition... Something like
<html:form onsubmit='return validate(this)'/>
and in the defintion
function validate(form) {
You can use the 'fields' attribute in the function to access the fields and their values as usual, like using the '.' precedence hierarchy for classes and objects. Think that this should have solved it.
When you use the struts-config.xml and define a <form-bean> and associate a particular <form-bean> with a action path, the logical name of the form bean becomes the name of the form when your jsp code gets converted to html. Then you can use javascript with the logical name of the form bean.

Use the DOM, say a getElementById("<Logical Name of the Form>").*

Hope this helps.
HI Deepthi,
Answers Here
1)You can use both the validation framework(JavaScript) as well as ur own javaScript functions..
if you want to validate ur fields, you can specify the same and validations of the perticular field in the validation.xml.
u can also do some operations on the same form using ur own functions..

2) u can refer the form object in the js using
document.getElementByName("form name");or document.getElementByID("form id");
before using the getElementByID make sure that ur form tag contains id attribute like this
<html:form action="/action path" name="form name" id="form id">

I think ur not giving action attribute in the <html:form> thats why it is throwing error, because <html:form> tag must have the action attribute.
3)try to see the html code of jsp then you will come to know, but i dont think that property treated as name.
better to use id for every thing and you ca access the same through js very easily.
<html:text property="userName" id="uname">
get the same in javascript by using
3)<html:text property="userName">
In the above can the property attribue of the element be treated as the name of the element and used in javascript??

According to my experience, it is possible. So, you may write "form.userName" in your JavaScript. But using an id attribute might be more secure, I think...

I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter

This thread has been viewed 691 times.

All times above are in ranch (not your local) time.
The current ranch time is
Feb 22, 2018 12:00:08.