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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking values of an array

Status
Not open for further replies.

Dronealone

IS-IT--Management
Mar 13, 2002
64
GB
Hello,

I want to do some simple error checking with Javascript. I have a form which contains the following element:

<input type='radio' name='answer[5]' value='17'>

and I am using the following function called by the onsubmit event to check that the radio has been checked:

function checkform (form)
{

if (form.answer[5].value == &quot;&quot;) {

alert( &quot;Please enter your first name.&quot; )
form.answer[5].focus();
return false;
}


return true;

}

However I constantly get an error answer.5 is null or not an object...

Can anybody help me here!!

Thanks.
 
You shouldn't name a form element &quot;answer[5]&quot;. If you want an array of form elements, just name them the same thing and the array will be created automatically...

<input name=&quot;answer&quot; value=&quot;1&quot;>
<input name=&quot;answer&quot; value=&quot;2&quot;>
<input name=&quot;answer&quot; value=&quot;3,000&quot;>

JavaScript will see these as answer[0], answer[1], and answer[2] on this page.


Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
I agree with mwolf00, you shouldn't name your element like that. But I know that technologies like PHP sometimes require that you name your elements like name=&quot;myElement[]&quot; in order to retrieve it as an array on the server side. I've never seen one where you need a number inside the brackets though. If you can't avoid it, you can refer to it like this:
document.formName[&quot;answer[5]&quot;].value

See faq216-3858 for more info on this technique.

Adam
 
I don't now PHP but in ASP you don't need the brackets. You just name all of the elements the same (as above) and when you retrieve them, they come as a comma separated list which you simply split into an array...

dim answer
answer = split(request(&quot;answer&quot;),&quot;,&quot;)

'now you have an array of form elements named &quot;anwer&quot;

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
What's also nice about ASP is that it'll assume it's an array without you splitting it if you use it in a loop.

<form method=&quot;post&quot;>
<input name=&quot;t&quot; value=&quot;a&quot;>
<input name=&quot;t&quot; value=&quot;b&quot;>
<input name=&quot;t&quot; value=&quot;c&quot;>
<input name=&quot;t&quot; value=&quot;d&quot;>
<input type=submit>
</form>
<%
For Each Item In Request.Form(&quot;t&quot;)
Response.Write Item & &quot;<br>&quot;
Next
%>

Adam
 
Nice tip!!!

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top