Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle condition in onClick?

 
S Man
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I have a list of rows in my jsp. Every row has the field "sequenceId". The user has the possibility to click on a navigation icon, placed at the beginning of every row, for further processing. Before further processing, I have to validate, if the User has entered a "sequenceId". I did the implementation with the following code:

<script type="text/javascript">
function validate()
{
var fsequenceId=document.getElementById("sequenceId").value
submitOK="true"

if (fsequenceId.length<1)
{
alert("Field sequenceId is mandatory.")
submitOK="false"
}
if (submitOK=="false")
{
return false
}
}
</script>


This is the function icon at the beginning of every row:

lt&;a href="#" onKlick='<%= "validate(); document.myForm" + addressSelectionListIndex + ".submit();" %>' />
<img border="0" src="<%= request.getContextPath() %>/images/Funktions_Icons/activ.gif" alt="<bean:message key="tooltip"/>">
</a>


Now I have 2 Problems:

1. My validate() method validates always the first "sequenceId" of the list. OK, I'll fix this later.

2. The Action (form submit) after the validate() method within the onClick Attrbite should only be processed, if the return Code of validate() was boolean TRUE. Any suggestions how I can do that?


Regards,
Marco
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding return keyword before the function call like:
"return validate();..."
 
S Man
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, after having added "return" I'm not able to submit my form any longer ?! It looks like that the form submit is never processed after having completed the validate() routine.

<a href="#" onKlick='<%= "return validate();document.newForm" + addressSelectionListIndex + ".submit();" %>' />
<img border="0" src="<%= request.getContextPath() %>/images/Funktions_Icons/tools_aktiv.gif" alt="<bean:message key="tooltip"/>">
</a>


Any suggestions?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well you are not returning true from your function.

Eric
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic