Reply
checkbox validation error...
Old 01-08-2006, 05:12 PM checkbox validation error...
Ultra Talker

Posts: 358
Location: Devon, UK
Trades: 4
I've been trying to get a checkbox validation working, I have tried several scripts and am coming into problems.

I want to make sure at least 1 and a maximum of 3 checkboxes from a group are selected.

The name of the checkboxes is r_genre[] and it has to be kept that way.

I think the square brackets at the end are causing the problem, as they are shown in blue in dreamweaver as opose to black as the rest of the text is.

Is there a way of including the brackets without causing a problem?

TIA
Simon
recedo is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 01-08-2006, 11:55 PM
AMysticWeb's Avatar
Experienced Talker

Posts: 38
Trades: 0
Hi Simon,

The script Here might be to your liking. Really good script source in general.
__________________
Stop by for FrontPage form tutorials
and form script examples. Mike
http://frontpageforms.com
AMysticWeb is offline
Reply With Quote
View Public Profile
 
Old 01-10-2006, 03:34 PM
Ultra Talker

Posts: 358
Location: Devon, UK
Trades: 4
I actually managed to find that one, unfortunatly it won't work either. I'm not sure if it's the square brackets or the fact that I hav other javascripts on the page, however, the last one I tried worked without the brackets fine.

Unfortunatly I need these so the form processor knows that an array is being sent.

Thanks for the link though. I think this may have to be done server side.
Simon
recedo is offline
Reply With Quote
View Public Profile
 
Old 01-14-2006, 06:45 PM
Extreme Talker

Posts: 160
Trades: 0
You can avoid referencing by name in your JavaScript so you don't have an issue with your php referencing. Just use the elements array. This is an array of the form's elements, here's an example :

function validate()
{
result=0;
start=0; // start of checkbox group
end=4; // end of checkbox group
for(i=start;i<end;i++)
{
result += document.forms[0].elements[i].checked ? 1:0;
}
if(result>0 && result<4)
{
alert("Fine");
return true;
}
else
{
alert("Problem");
return false;
}
}
ElectricSheep is offline
Reply With Quote
View Public Profile
 
Old 01-14-2006, 11:53 PM
Ultra Talker

Posts: 358
Location: Devon, UK
Trades: 4
Thanks for that code. Can you please explain a little better what this actually does... Would I just stick that script in the head and call the function with 'onsubmit' of the form?

Sorry, I'm a novice.
Simon
recedo is offline
Reply With Quote
View Public Profile
 
Old 01-15-2006, 01:19 PM
Extreme Talker

Posts: 160
Trades: 0
Please delete

Last edited by ElectricSheep; 01-15-2006 at 01:24 PM..
ElectricSheep is offline
Reply With Quote
View Public Profile
 
Old 01-15-2006, 01:23 PM
Extreme Talker

Posts: 160
Trades: 0
Sorry, I should have explained it better. I took it you might adapt it to your own needs.

Yes you do call it 'onsubmit' of the form so your opening form tag will need this :

onsubmit="return validate()"

The elements array allows the code to reference every element of the form, for example the first element in the form would be elements[0].

I have commented the script a little better below :

function validate()
{
result=0; // variable to hold number of ticked boxes, start with zero
start=0; // variable to hold position in form of first checkbox in group
end=4; // variable to hold position in form of last checkbox in group
for(i=start;i<end;i++)
{
// find if each box is checked, if it is add one to the result variable, else add zero
// if this is the only form then you can reference it as forms[0]
result += document.forms[0].elements[i].checked ? 1:0;
}
// check if result variable holds a valid number ( 1,2 or 3 )
if(result>0 && result<4)
{
alert("Fine");
return true; // form will be submitted
}
else
{
alert("Problem");
return false; // form will not be submitted
}
}

This approach can be developed further depending on the complexity of the form. For example, the elements array can be used to test the type attribute and act accordingly if you need to cycle through the entire form.

Here is what the form may look like, this would need no alterations to the validate() function above :

<form onsubmit="return validate()" method="post" action="somescript.tcl">
<input name="r_genre[]" type="checkbox">
<input name="r_genre[]" type="checkbox">
<input name="r_genre[]" type="checkbox">
<input name="r_genre[]" type="checkbox">
<input type="submit">
</form>
ElectricSheep is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to checkbox validation error...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.12757 seconds with 13 queries