radiobutton form validation

D

Dirk Bollen

Hi,

I'm new to javascript and I want to validate the fields in a html form.
I have written a function to validate my radiobuttons but it seems it
doesn't work.

First I put the form output in a var using : document.getElementById
Than make a functioncall in a nested loop to check all my fields in a form.

The radiobutton check gives an error. It seems that elem.lengt does not
gives the lenght of the array? Putting an alert(elem.length); to check
the output gives 'undefined' message.

What goes wrong?

var gender = document.getElementById('gender');
.......
if(isNumeric(age, "Please enter your age")){
if(isRadio(gender, "Please select a gender")){ ....


//this is the validation function

function isRadio(elem, helperMsg){
//check for array length
alert(elem.length);

for(i=0;i<elem.length;i++){
alert(elem.checked);
if(elem.checked){
return true;
}
}

alert(helperMsg);
elem.focus();
return false;
}
 
D

Dr J R Stockton

In comp.lang.javascript message said:
I'm new to javascript and I want to validate the fields in a html form.
I have written a function to validate my radiobuttons but it seems it
doesn't work.

A set of radiobuttons should always be valid, unless no default was set
and no button was ever clicked.

Consider, therefore, the following code, in which the button performs
and shows the test.

<input type=radio name=X checked style="display:none;">
<input type=radio name=X>
<input type=radio name=X>
<input type=radio name=X>
<input type=button onClick="this.value=!X[0].checked">

Remember while coding that the visible buttons are numbered 1, 2, ...



<G> <input type=button value="Do Not Press"
onClick='document.body.style.display="none"'> </G>

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

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,772
Messages
2,569,593
Members
45,104
Latest member
LesliVqm09
Top