This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes JSP and the fly likes How to get a scriptlet value for javascript validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How to get a scriptlet value for javascript validation" Watch "How to get a scriptlet value for javascript validation" New topic

How to get a scriptlet value for javascript validation

hrin kuek

Joined: Mar 24, 2005
Posts: 9
Hi there,

I need to ensure that at least one checkbox in my whole list of dynamic checkboxes are ticked. I think the problem is I don't know how to get the input name value out so that I can use javascript.

Hope someone can enlighten me. Hope to hear from you guys soon!

Appreciate the help..

Danielle Hrin
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63052

I'm having a hard time understanding what your question is. Please elucidate.

Also, it not necessary, and is very confusing, to mangle your code for posting. it is very straight-forward to just enter:

Otherwise, we have no way of knowing what your code really looks like.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
hrin kuek

Joined: Mar 24, 2005
Posts: 9
oops sorry for the confusion... The code is the way you have written it.

The thing is I have this javascript function that goes like this:

so that it will be able to look thru the whole list of my dynamic checkboxes to make sure I have ticked at least one checkbox. But it doesn't seem to work. Is it cos scriptlet is server-side scripting while javascipt is client-side? Is there anyway I can get around it?
hrin kuek

Joined: Mar 24, 2005
Posts: 9
The whole code is like that:

Stefan Evans

Joined: Jul 06, 2005
Posts: 1517
A mix of JSP/javascript. Possibly more javascript than JSP, but there are some dynamic elements to it.

I'm not sure exactly what the name of the checkbox is.
In your first post it is name="<%= modCode %>"
in the full code it is name="modCode"
Which is it?

From what I can see you have a list of modules.
Each module contains a list of tasks.
You generate a checkbox for each task in a module.

My assumption:
The validator has to make sure that for each moduleone checkbox is checked.

The javascript:
Here is a javascript function that will check to see if any checkboxes are checked for all the checkboxes in one group.
It also accounts for the cases where there are
- no checkboxes
- one checkbox
- multiple checkboxes.

Now the java/jsp bit.
You just need to call this function from your current validSecurityClass() function.

in the case that you only need one checkbox checked out of all of the checkboxes on the page, and it is name="modCode"
then this will do the trick:

var checkBoxChecked = validateCheckBoxes(thisForm, "modCode");

If it is name="<%= modCode %>"

you need to generate multiple calls to make sure that for each module, at least one task is clicked:

var checkBoxChecked = "true";
// loop for Each module:
checkBoxChecked = checkBoxChecked && validateCheckBoxes(thisForm, "<%= modCode %>");

which will generate code like this:

The final thing I would suggest is to use JSTL for looping rather than scriptlet code. It would be much neater than scriptlet code ;-)

Good luck,
jQuery in Action, 3rd edition
subject: How to get a scriptlet value for javascript validation
It's not a secret anymore!