rusco
asked on
Struts and Javascript
Hi experts,
Is there a way for struts to first call a javascript function (to check some value) and if it returns a certain value, continue on with the action, or if returns another value, simply display a javascript alert window and not enter the action anymore?
I have a form and a submit button (an image button), but I wanted to try and perform a validation check first using javascript and inform the user using an alert window if the value = false. If value = true, continue with the action.
Thanks!
Is there a way for struts to first call a javascript function (to check some value) and if it returns a certain value, continue on with the action, or if returns another value, simply display a javascript alert window and not enter the action anymore?
I have a form and a submit button (an image button), but I wanted to try and perform a validation check first using javascript and inform the user using an alert window if the value = false. If value = true, continue with the action.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Should be
document.yourForm.submit() ;
document.yourForm.submit()
ASKER
Hi Didier!
I tried out what you said but I run into some problems with javascript. It seems that when my code goes to my javascript, it only reads the first line of code:
ex:
my function {
alert("Hello");
alert("Test");
}
what will be returned to me is just the alert "Hello", the next alert will not be shown anymore. Below is my actual jsp code to do this (I forgot that I will be opening a new popup for this one):
<html:image src="/ADM/images/edit_butt on.gif" border="0" onclick="return false"/>
<script language="javascript"><!-- begin
function getConfigItemKey()
{
var count = parent.configItemsListForm .counter.v alue;
var newWindow;
alert(count);
if (count.value == 1) {
if (newWindow) {
newWindow.close();
newWindow = null;
}
var test = 1;
window.open('/ADM/SetupEdi tConfigIte m.do?
configItemKey=test','confi rm','width =400,heigh t=150');
}
if (count.value == 0) {
alert("Please select a config item to edit/view");
}
if (count.value > 1) {
alert("Please select only one config item to edit/view");
}
}
\\ end-->
</script>
Thanks!
I tried out what you said but I run into some problems with javascript. It seems that when my code goes to my javascript, it only reads the first line of code:
ex:
my function {
alert("Hello");
alert("Test");
}
what will be returned to me is just the alert "Hello", the next alert will not be shown anymore. Below is my actual jsp code to do this (I forgot that I will be opening a new popup for this one):
<html:image src="/ADM/images/edit_butt
<script language="javascript"><!--
function getConfigItemKey()
{
var count = parent.configItemsListForm
var newWindow;
alert(count);
if (count.value == 1) {
if (newWindow) {
newWindow.close();
newWindow = null;
}
var test = 1;
window.open('/ADM/SetupEdi
configItemKey=test','confi
}
if (count.value == 0) {
alert("Please select a config item to edit/view");
}
if (count.value > 1) {
alert("Please select only one config item to edit/view");
}
}
\\ end-->
</script>
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For example:
<script>
function myFunction(){
//do your validation here
if (yourValidationFunction())
document.yourForm.action="
document.submit();
} else{
alert("wrong input");
}
}
</script>
<input type="button" onClick="myfunction()">
But there is a Struts Validator for this. I used it a while ago and it works perfectly.
There are already some basic validators available. The mask can be very usefull. But you can also add your own (Javascript) to the validator-rules.xml.
For more information about this see http://jakarta.apache.org/struts/userGuide/dev_validator.html. On the bottom of this page you can see a ref to http://www.manning-source.com/books/husted/husted_ch12.pdf. Look for client side validation.
Greetz,
Didier