HighDots Forums  

newbie, checking to make sure a radio button is checked

Javascript JavaScript language (comp.lang.javascript)


Discuss newbie, checking to make sure a radio button is checked in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bob Imperial
 
Posts: n/a

Default newbie, checking to make sure a radio button is checked - 01-29-2008 , 12:18 PM






Hi folks,

This is my first time posting here and I am hoping that I can get my
head around what I am sure is basic for most of you here. I have the
following script I am using to do some basic form validation and I've
run into a chunk that I'm just not getting. I just want to check to
make sure one of the radio buttons has been checked within my current
script. Any help would be greatly appreciated!

<script type="text/javascript">
function validateForm() {
var allOk = true;
var errorString = "The following are required fields:\n";
var counter = 0;

// require first name
if (!document.studyRegister.FirstName.value) {
allOk = false;
errorString += "\n- First Name";
}
// require last name
if (!document.studyRegister.LastName.value) {
allOk = false;
errorString += "\n- Last Name";
}

// check to make sure they choose a gender
for (counter < document.studyRegister.gender.length; counter++)
{
if(!document.studyRegister.gender[counter].checked)
allOk = false;
errorString += "\n- Gender";
}

// require date of birth
if (!document.studyRegister.dob.value) {
allOk = false;
errorString += "\n- Date of birth";
}

if (!allOk)
{
alert(errorString);
return false;
}

return allOk;
}
//-->
</script>

<form name="studyRegister" action="dump.cfm" method="post"
enctype="application/x-www-form-urlencoded" onsubmit="return
validateForm();">
<input type="text" name="FirstName" value="" />
<input type="text" name="LastName" value="" />
<input type="radio" name="gender" value="male" />&nbsp;Female <input
type="radio" name="gender" value="female" />
<input type="text" name="dob" value="" />
</form>

At present it just blows past the script completely and I know it's
the code block for checking the radio buttons, just cannot figure out
how to get this done within my existing script.

Bewildered Bob

Reply With Quote
  #2  
Old   
Michael White
 
Posts: n/a

Default Re: newbie, checking to make sure a radio button is checked - 01-29-2008 , 01:33 PM






Bob Imperial wrote:

Quote:
Hi folks,

This is my first time posting here and I am hoping that I can get my
head around what I am sure is basic for most of you here. I have the
following script I am using to do some basic form validation and I've
run into a chunk that I'm just not getting. I just want to check to
make sure one of the radio buttons has been checked within my current
script. Any help would be greatly appreciated!

script type="text/javascript"
function validateForm() {
var allOk = true;
var errorString = "The following are required fields:\n";
var counter = 0;

// require first name
if (!document.studyRegister.FirstName.value) {
allOk = false;
errorString += "\n- First Name";
}
// require last name
if (!document.studyRegister.LastName.value) {
allOk = false;
errorString += "\n- Last Name";
}


// check to make sure they choose a gender
for (counter < document.studyRegister.gender.length; counter++)
{
if(!document.studyRegister.gender[counter].checked)
allOk = false;
errorString += "\n- Gender";
}
The above is missing some curly braces, and is illogical:

var g=document.studyRegister.gender,gLen=g.length,c;
for (var counter=0,c=0;counter<gLen;counter++){
if(g[counter].checked)c++
}
if(!c){allOk=false;errorString+="\n- Gender";}

But your whole concept is unwieldly.
Why not pass the form to the function:

<script type="text/javascript">
function validateForm(form){
var errorString = "The following are required fields:\n",error="",c;
if(!form.FirstName.value) error+="First Name";
if(!form.LastName.value) error+="\n- Last Name";
for (var counter=0,c=0,counter<form.gender.length; counter++){
if(g[counter].checked)c++;
}
if(!c){error+="\n- Gender";}
if(!form.dob.value) error+="\n- Date of birth";
if(error){
alert("The following are required fields:\n"+error);
return false;
}
return true;
}
</script>

<form ... onsubmit="return validateForm(this);">


Mick


Reply With Quote
  #3  
Old   
Bob Imperial
 
Posts: n/a

Default Re: newbie, checking to make sure a radio button is checked - 01-29-2008 , 02:28 PM



On Jan 29, 1:33 pm, Michael White <m... (AT) mickweb (DOT) com> wrote:
Quote:
Bob Imperial wrote:
Hi folks,

This is my first time posting here and I am hoping that I can get my
head around what I am sure is basic for most of you here. I have the
following script I am using to do some basic form validation and I've
run into a chunk that I'm just not getting. I just want to check to
make sure one of the radio buttons has been checked within my current
script. Any help would be greatly appreciated!

script type="text/javascript"
function validateForm() {
var allOk = true;
var errorString = "The following are required fields:\n";
var counter = 0;

// require first name
if (!document.studyRegister.FirstName.value) {
allOk = false;
errorString += "\n- First Name";
}
// require last name
if (!document.studyRegister.LastName.value) {
allOk = false;
errorString += "\n- Last Name";
}

// check to make sure they choose a gender
for (counter < document.studyRegister.gender.length; counter++)
{
if(!document.studyRegister.gender[counter].checked)
allOk = false;
errorString += "\n- Gender";
}

The above is missing some curly braces, and is illogical:

var g=document.studyRegister.gender,gLen=g.length,c;
for (var counter=0,c=0;counter<gLen;counter++){
if(g[counter].checked)c++}

if(!c){allOk=false;errorString+="\n- Gender";}

But your whole concept is unwieldly.
Why not pass the form to the function:

script type="text/javascript"
function validateForm(form){
var errorString = "The following are required fields:\n",error="",c;
if(!form.FirstName.value) error+="First Name";
if(!form.LastName.value) error+="\n- Last Name";
for (var counter=0,c=0,counter<form.gender.length; counter++){
if(g[counter].checked)c++;
}
if(!c){error+="\n- Gender";}
if(!form.dob.value) error+="\n- Date of birth";
if(error){
alert("The following are required fields:\n"+error);
return false;
}
return true;}

/script

form ... onsubmit="return validateForm(this);"

Mick
Mick,

While I appreciate your time and effort what you've offered here
doesn't seem to work or I am not doing something right here which is
very possible since I am new to working with js. It just blows by the
script altogether. The script I was using is one a friend passed on to
me a while back and while it may not follow yours it has worked for me
w/o my block of hodgepodge I tried to add for the radio buttons.
<script type="text/javascript">
function validateForm() {
var allOk = true;
var errorString = "The following are required fields:
\n";

// require first name
if (!document.studyRegister.FirstName.value) {
allOk = false;
errorString += "\n- First Name";
}
// require last name
if (!document.studyRegister.LastName.value) {
allOk = false;
errorString += "\n- Last Name";

// require date of birth
if (!document.studyRegister.dob.value) {
allOk = false;
errorString += "\n- Date of birth";
}

if (!allOk)
{
alert(errorString);
return false;
}

return allOk;
}
</script>

Does work however ugly it may be and it's simple enough for my feeble
mind to grasp what it's doing. I'm just wondering if the loop can in
fact be done within my current script?

Thanks again for your time and effort!

Bob


Reply With Quote
  #4  
Old   
Michael White
 
Posts: n/a

Default Re: newbie, checking to make sure a radio button is checked - 01-29-2008 , 04:58 PM



Bob Imperial wrote:


Quote:
Mick,

While I appreciate your time and effort what you've offered here
doesn't seem to work or I am not doing something right here which is
very possible since I am new to working with js. It just blows by the
script altogether. The script I was using is one a friend passed on to
me a while back and while it may not follow yours it has worked for me
w/o my block of hodgepodge I tried to add for the radio buttons.
script type="text/javascript"
function validateForm() {
var allOk = true;
var errorString = "The following are required fields:
\n";

// require first name
if (!document.studyRegister.FirstName.value) {
allOk = false;
errorString += "\n- First Name";
}
// require last name
if (!document.studyRegister.LastName.value) {
allOk = false;
errorString += "\n- Last Name";
Missing "}"

Quote:
// require date of birth
if (!document.studyRegister.dob.value) {
allOk = false;
errorString += "\n- Date of birth";
}

if (!allOk)
{
alert(errorString);
return false;
}

return allOk;
}
/script

Does work however ugly it may be and it's simple enough for my feeble
mind to grasp what it's doing. I'm just wondering if the loop can in
fact be done within my current script?

Thanks again for your time and effort!

Bob
Mick


Reply With Quote
  #5  
Old   
Hal Rosser
 
Posts: n/a

Default Re: newbie, checking to make sure a radio button is checked - 01-29-2008 , 11:56 PM




"Bob Imperial" <bimperial (AT) gmail (DOT) com> wrote

Quote:
Hi folks,

This is my first time posting here and I am hoping that I can get my
head around what I am sure is basic for most of you here. I have the
following script I am using to do some basic form validation and I've
run into a chunk that I'm just not getting. I just want to check to
make sure one of the radio buttons has been checked within my current
script. Any help would be greatly appreciated!

Why validate a radio button ? Pick one yourself.
Pick one of each 'group' and add the "checked" attribute to the most
commonly selected choice of each.
The user will select a different one (or not). and one of each group will be
checked by default.






Reply With Quote
Reply




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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.