![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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"; } |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 Mick |
#5
| |||
| |||
|
|
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! |
![]() |
| Thread Tools | |
| Display Modes | |
| |