Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Radio Button Verify 1

Status
Not open for further replies.

ID10T16

Technical User
Sep 22, 2000
160
US
Ok, I must be having a brainfart because I'm almost certain I've done this before in a fairly similar way, but I cannot get it to work now.

Here's the situation:

I have a survey form that has some required fields that I verify are filled in onSubmit. I'm trying to use one script function to check and make sure that a selection is chosen from the the radio selections. Unfortunately it doesn't work, and I'm sure it's because I'm sending or calling a variable incorrectly, but I can't find a way to test it out & see what the variable's value is.

Here's my code:

function validate() {
var errormessage = new String();
if(isEmpty(document.survey.why_rating.value))
{ errormessage += "\n\n Please enter why you rated the website the way you did."; }
if(Checker("survey.current_rating.checked"))
{ errormessage += "\n\n Please rate the current website."; }
if(Checker("survey.visit.checked"))
{ errormessage += "\n\n Please tell us how often you visit the website currently."; }
if(Checker("survey.age.checked"))
{ errormessage += "\n\n Please tell us your age range."; }
if(Checker("survey.marital.checked"))
{ errormessage += "\n\n Please tell us your marital status."; }
if(Checker("survey.gender.checked"))
{ errormessage += "\n\n Please select a gender."; }
if(Checker("survey.internet.checked"))
{ errormessage += "\n\n Please tell us your primary internet connection type"; }
if(errormessage.length > 2) {
alert('NOTE:' + errormessage);
return false;
}
return true;
}


function isEmpty(ss) {
if(ss.length > 0) {
return false; }
return true;
}

function Checker(q) {
var ret=true;
for(i=0; i > 5; i++)
{
if (q)
{
ret=false;
}
}
return ret;
}


Here's some of the form:


<script src="*edit*/survey/survey2.js">
</script>
<form action="*edit*/survey/surveythankyou.php" name="survey" onSubmit="return validate();">
<b>Your Name: </b>
(optional)<br />
<input type="text" name="dept" size="50" maxlength="255"/>
<br />
<b>E-mail Address:</b>(optional)
<br />
<input type="text" name="name" size="50" maxlength="255"/>
<br />
<br />
<b>How old are you?</b><font color="red">*</font>
<br />
<br />
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>12 and under</td><td>13-17</td><td>18-25</td><td>26-39</td><td>40-59</td><td>60 and over</td></tr><tr align="center"><td>
<input type="radio" name="age" id="age" value="1"/></td><td><input type="radio" name="age" id="age" value="2"/></td><td><input type="radio" name="age" id="age" value="3"/></td><td><input type="radio" name="age" id="age" value="4"/></td><td><input type="radio" name="age" id="age" value="5"/></td><td><input type="radio" name="age" id="age" value="6"/></td></tr></table>
<br />
<br />
<b>Gender?</b><font color="red">*</font>
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>Male</td><td>Female</td></tr><tr align="center"><td>
<input type="radio" name="gender" id="gender" value="M"/></td><td><input type="radio" name="gender" id="gender" value="F"/></td></tr></table>
<br />
<br />
<b>Are you married or single?</b><font color="red">*</font>
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>Single</td><td>Married</td></tr><tr align="center"><td>
<input type="radio" name="marital" id="marital" value="Single"/></td><td><input type="radio" name="marital" id="marital" value="Married"/></td></tr></table>
<br />
<br />
<b>What type of internet connection do you use?</b><font color="red">*</font>
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>Broadband</td><td>Dial-up</td></tr><tr align="center"><td>
<input type="radio" name="internet" id="internet" value="B"/></td><td><input type="radio" name="internet" id="internet" value="D"/></td></tr></table>
<br />
<br />
<b>How often do you visit the website currently? (Please choose what closest describes you):</b><font color="red">*</font><br /><br />
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>This is my first time here</td><td>Once a year</td><td>Once a month</td><td>Once a week</td><td>At least once a day</td></tr><tr align="center"><td>
<input type="radio" name="visit" id="visit" value="1"/></td><td><input type="radio" name="visit" id="visit" value="2"/></td><td><input type="radio" name="visit" id="visit" value="3"/></td><td><input type="radio" name="visit" id="visit" value="4"/></td><td><input type="radio" name="visit" id="visit" value="5"/></td></tr></table><br /><br />
<b>How would you rate the current website overall (appearance, functionality, ease of use, etc?):</b><font color="red">*</font><br /><br />
<table border="1" bordercolor="#CCCCCC"><tr align="center"><td>We'd be better off with nothing</td><td>Better than nothing</td><td>Adequate</td><td>Pretty Good</td><td>One of the best you've ever seen</td></tr><tr align="center"><td>
<input type="radio" name="current_rating" value="1"/></td><td><input type="radio" name="current_rating" id="current_rating" value="2"/></td><td><input type="radio" name="current_rating" id="current_rating" value="3"/></td><td><input type="radio" name="current_rating" id="current_rating" value="4"/></td><td><input type="radio" name="current_rating" id="current_rating" value="5"/></td></tr></table><br />
<b>Why:</b><font color="red">*</font> <br />
<textarea name="why_rating" rows="5" cols="50"></textarea>
<br />


I don't know what stupid thing I'm doing, or not doing, but it is frustrating me.

The deal is, I could easily get this done via php, but I ALWAYS like to know what I'm doing incorrectly. I tend to learn best that way.

Thanks in advance to anyone that can help me out.
 
Things that are wrong with your code include:

- Having multiple elements with the same ID. This is not good, and would be flagged as invalid by any decent HTML validator (so assume you've not done this yet).

- You are passing a string to try and refer to a form element. Personally, I'd add this one line at the very top of the validate function:

Code:
var frm = document.forms['survey'].elements;

and then change all occurrences of this:

Code:
document.survey.

to this:

Code:
frm.

and all occurrences of

Code:
"survey.visit[i].checked"

to this:

Code:
frm['visit'][i].checked"

(obviously replacing 'visit' with the other names, too).

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
nice one dan, can you tel me though I've started using this method to short cut referencing things, is it OK.

var a = document.getElementById('id_of_form');

if(a.input_field.checked){do what ever;}

or

if(a.input_field.value == 'xyz'){do what ever;}

and even (if the get element bit was on a DIV's ID!)

a.innerHTML = 'xyz';


It all works fine with no errors and seems to be a great shortcut in coding, what's your thoughts?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top