Validation with javascript of two dimensional array

K

karmenkrile

In html i have some question, and the answers are radio buttons ...
the names of variables in html are array ... like question[1],
question[2] ... etc.
and every question has multiple value, depending of the answer.


HTML Code:

<form action="thx.php" method=POST name=form onSubmit="return
check(this)">



In javascript i have to determinate if the question is answered, if it
is not then return false ...

Code:

function check(form) { a=0; for (i=0;i<form.question[1].length;i++){ if
(form.question[1].checked===true) {a=1;} } if (a==0) {
alert("question[1] no.1: You didn't answer the question"); return
false; } }


Everything was working fine until i changed my variables into arrays.
What is wrong now?
How javascript handle two dimensional array?

pls help
 
R

Richard Cornford

In html i have some question, and the answers are radio
buttons ... the names of variables in html are array ...

There are no arrays in HTML, it is a mark-up language not a programming
language. The value of a NAME attribute is just a sequence of
characters, with no additional meaning.
like question[1], question[2] ... etc.
and every question has multiple value, depending of
the answer.

HTML Code:

<form action="thx.php" method=POST name=form onSubmit="return
check(this)">

There are no said:
In javascript i have to determinate if the question is
answered, if it is not then return false ...

Code:

function check(form) { a=0; for (i=0;i<form.question[1].length;i++){
if (form.question[1].checked===true) {a=1;} } if (a==0) {


In javascript the code - question[1] - is meaningful, it is a bracket
notation property accessor. As the character sequence 'question[1]' had
no meaning in HTML there is nothing in the DOM that makes sense to
javascript's understanding of the property accessor. However, there is a
property of the form's - elements - collection that has the name
'questions[1]' and it can be referenced by name as:-

form.elements["question[1]"]

See:-

alert("question[1] no.1: You didn't answer the question"); return
false; } }


Everything was working fine until i changed my variables into
arrays. What is wrong now?

You changed the name from a sequence of characters that could be a
javascript Identifier, and so could be used in a dot notation property
accessor, into a sequence of characters that resembled a bracket
notation property accessor, and so had very different meaning when
inserted into a dot-notation property accessor.
How javascript handle two dimensional array?

That is irrelevant as the subject of your question is a Collection not
an array, and it remains one dimensional.

Richard.
 
K

karmenkrile

thank you very very much :))
it works now :)

Richard said:
In html i have some question, and the answers are radio
buttons ... the names of variables in html are array ...

There are no arrays in HTML, it is a mark-up language not a programming
language. The value of a NAME attribute is just a sequence of
characters, with no additional meaning.
like question[1], question[2] ... etc.
and every question has multiple value, depending of
the answer.

HTML Code:

<form action="thx.php" method=POST name=form onSubmit="return
check(this)">

There are no said:
In javascript i have to determinate if the question is
answered, if it is not then return false ...

Code:

function check(form) { a=0; for (i=0;i<form.question[1].length;i++){
if (form.question[1].checked===true) {a=1;} } if (a==0) {


In javascript the code - question[1] - is meaningful, it is a bracket
notation property accessor. As the character sequence 'question[1]' had
no meaning in HTML there is nothing in the DOM that makes sense to
javascript's understanding of the property accessor. However, there is a
property of the form's - elements - collection that has the name
'questions[1]' and it can be referenced by name as:-

form.elements["question[1]"]

See:-

alert("question[1] no.1: You didn't answer the question"); return
false; } }


Everything was working fine until i changed my variables into
arrays. What is wrong now?

You changed the name from a sequence of characters that could be a
javascript Identifier, and so could be used in a dot notation property
accessor, into a sequence of characters that resembled a bracket
notation property accessor, and so had very different meaning when
inserted into a dot-notation property accessor.
How javascript handle two dimensional array?

That is irrelevant as the subject of your question is a Collection not
an array, and it remains one dimensional.

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top