Current location: Hot Scripts Forums » General Web Coding » JavaScript » Flexible form validation question


Flexible form validation question

Reply
  #1 (permalink)  
Old 09-10-04, 02:52 AM
epetoke epetoke is offline
New Member
 
Join Date: May 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Flexible form validation question

Hi, can anyone help me with a form validation problem i'm having.

I have this form which has multiple check boxes and i don't want people to be able to submit the form without checking at least one checkbox.

This one keeps getting me stumped because of course i can easily write a script to force users to select certain checkboxes before they can submit the form but when it comes to forcing them to select any one before they submit it all crashes on me.

If you know of a piece of code i can look at that will do the job for me i will be grateful.
Reply With Quote
  #2 (permalink)  
Old 09-10-04, 09:58 PM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
If your willing to switch to php i can o it for you...
Reply With Quote
  #3 (permalink)  
Old 09-10-04, 11:06 PM
whizmail whizmail is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Hi epetoke,

Could you let us know the URL? I'll look into it. I create form validations for a living

Best Regards,

Raphael Pirker
raphaelp@whiz-mail.cc
http://www.whiz-mail.cc
Reply With Quote
  #4 (permalink)  
Old 09-11-04, 05:54 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Validation Script

This script will go through all the checkboxes in a given form and make sure at least one is checked. If none are checked it sends an alert. To make it work just insert the name of your form everywhere that it says 'theform'.
Code:
<script>

function validatechecks()
{
var elmtlength = window.document.theform.elements.length
var totalchecked = 0
var i;

for (i = 0; i < elmtlength; i ++)
{


if(window.document.theform.elements[i].type == 'checkbox')
{

if(window.document.theform.elements[i].checked == true)
{

totalchecked = totalchecked + 1

}

}

}

if(totalchecked < 1)
{
alert('You must check at least one option before proceeding')
}

}

</script>
Reply With Quote
  #5 (permalink)  
Old 09-12-04, 08:01 AM
Eclipse's Avatar
Eclipse Eclipse is offline
Coding Addict
 
Join Date: May 2004
Location: Long Island, New York
Posts: 356
Thanks: 0
Thanked 0 Times in 0 Posts
then wouldn't you need to have: <form type="submit" value="Submut Form" onclick="validatechecks()">
Reply With Quote
  #6 (permalink)  
Old 09-12-04, 01:08 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Add this

To trigger it just add this into your form:

<input type="button" value="Submit" onClick="validatechecks()">

and add this into the script right after the last if statement.

else
{
window.location='theURL'
}

If you use <input type="Submit" it doesn't work as well

When the user clicks on the button if the checkboxes aren't checked it will alert and if they are it will proceed to the page u specified in the else{} statement.
Reply With Quote
  #7 (permalink)  
Old 09-12-04, 04:19 PM
whizmail whizmail is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
When the user clicks on the button if the checkboxes aren't checked it will alert and if they are it will proceed to the page u specified in the else{} statement.
... without submitting the information, though, if I'm not mistaken?

Here's my 5c: (javascript is copyrighted by dwoody!)

Code:
<script>

function validatechecks()
{
var elmtlength = window.document.theform.elements.length
var totalchecked = 0
var i;

for (i = 0; i < elmtlength; i ++)
{

if(window.document.theform.elements[i].type == 'checkbox')
{

if(window.document.theform.elements[i].checked == true)
{

totalchecked = totalchecked + 1

}

}

}

if(totalchecked < 1)
{
alert('You must check at least one option before proceeding');
return false;
} else {
return true;
}

}

</script>

[...rest of page bjabjabja...]

<form [...] onSubmit="return validatechecks();">

Last edited by whizmail; 09-12-04 at 04:21 PM. Reason: forgot the quote
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
form validation ( formCheck(this);submitCheck() ) Dude21 JavaScript 0 08-31-04 09:15 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Form validation - required fields Funky_Monk JavaScript 1 08-16-04 10:26 AM
form question delerium PHP 5 01-19-04 10:30 PM
question on form and submit TxRanger JavaScript 2 11-03-03 11:28 AM


All times are GMT -5. The time now is 10:30 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.