Javascript: Validating an array based form...
I am having trouble validating an array based form.
Normally in my validator script I would use this (simplified):
function validateForm(theForm)
{
if (!radioCheck(theForm.q1,"Question 1",true)) return false;
}
Anyways, for this form my question fields are named for use as arrays:
<INPUT type=radio value=1 name="q[6]">
And the problem is when I try these:
if (!radioCheck(theForm.q[1],"Question 1",true)) return false;
if (!radioCheck(theForm."q[1]","Question 1",true)) return false;
if (!radioCheck(theForm.getElementById("q[1]"),"Question 1",true)) return false;
They don't work. I'm not sure how to do this, if it is even possible at all.
Abhishek Reddy posted this at 07:49 — 22nd August 2004.
He has: 3,348 posts
Joined: Jul 2001
Can you show us the whole page?
abudabit posted this at 09:00 — 22nd August 2004.
They have: 26 posts
Joined: Aug 2004
It's pretty large. But here is a version of the page with only 2 questions (the real one has tons):
<script Language="JavaScript">
function radioCheck(formField,fieldLabel,required)
{
var result = false;
for(var j = 0 ; j < formField.length ; ++j) {
if(formField[j].checked) {
result = true;
}
}
if (!result) {
result = false;
alert('Please make a selection for ' + fieldLabel +'.');
}
return result;
}
function validateForm(theForm)
{
if (!radioCheck(theForm.q[1),"Question 1",true)) return false;
if (!radioCheck(theForm.q[2],"Question 2",true)) return false;
return true;
}
</script>
<FORM name=gadgets action='gadgets?gadget=Personality' method='post' onsubmit='return validateForm(this)' id=create>
<B>Question 1......</B><BR>
<INPUT type=radio value=0 name="q[1]"> Not At All
<BR><INPUT type=radio value=1 name="q[1]"> A Bit
<BR><INPUT type=radio value=2 name="q[1]"> Somewhat
<BR><INPUT type=radio value=3 name="q[1]"> True
<BR><INPUT type=radio value=4 name="q[1]"> Very True
<BR><BR>
<B>Question 2.....</B><BR>
<INPUT type=radio value=0 name="q[2]"> Not At All
<BR><INPUT type=radio value=1 name="q[2]"> A Bit
<BR><INPUT type=radio value=2 name="q[2]"> Somewhat
<BR><INPUT type=radio value=3 name="q[2]"> True
<BR><INPUT type=radio value=4 name="q[2]"> Very True
<BR><BR>
<INPUT type=submit name="submit" value="Submit">
</FORM>
Now with normally named radio buttons this works, but with the q[arraypointer] named buttons it doesn't. What it does normally is when you submit the form the ValidateForm function runs the radioCheck function for each button set. The error is occuring with I believe javascript not being able to handle the format of theForm.array[pointer]. Normally you would do theForm.q1 instead of theForm.q[1] and that would work but I need q[1] for php purposes.
Abhishek Reddy posted this at 10:21 — 22nd August 2004.
He has: 3,348 posts
Joined: Jul 2001
if (!radioCheck(theForm.q[1),"Question 1",true)) return false;
'Notice the incorrect bracket there? Change q[1) to q[1].
abudabit posted this at 04:07 — 23rd August 2004.
They have: 26 posts
Joined: Aug 2004
That was an error on this side of things. Didn't have that error in my script.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.