HighDots Forums  

radiobutton form validation

Javascript JavaScript language (comp.lang.javascript)


Discuss radiobutton form validation in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dirk Bollen
 
Posts: n/a

Default radiobutton form validation - 09-25-2007 , 05:24 PM






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[i]);
if(elem.checked[i]){
return true;
}
}

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


Reply With Quote
  #2  
Old   
Randy Webb
 
Posts: n/a

Default Re: radiobutton form validation - 09-25-2007 , 06:38 PM






Dirk Bollen said the following on 9/25/2007 5:24 PM:
Quote:
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){

How do I get the value of a form control?
<URL: http://jibbering.com/faq/index.html#FAQ4_13>

And the Notes page it links to:

<URL: http://www.jibbering.com/faq/faq_notes/form_access.html#faBut>

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #3  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: radiobutton form validation - 09-26-2007 , 07:26 AM



In comp.lang.javascript message <2007092523243716807-dirkheld@gmailcom>,
Tue, 25 Sep 2007 21:24:41, Dirk Bollen <dirkheld (AT) gmail (DOT) com> posted:
Quote:
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.