Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: form validation


Message #1 by "William Kim" <william_kim@m...> on Tue, 10 Apr 2001 19:59:16
The value of an unchecked radio button is not "" - use the checked 
property instead, like this generic function:

function isRadioChosen(radioObject, msg) {
	var val = false;
	for (var i=0; i<radioObject.length; i++) {
		if (radioObject[i].checked) {
			val = true;
			break;
		}
	}
	if (!val)
		alert(msg);
}

Then from HTML:
<FORM action="" method=post id=form1 name=form1>
<INPUT TYPE="radio" NAME="BrowserTestYN" ID="BrowserTestYN" VALUE=1> Yes  
<br><INPUT TYPE="radio" NAME="BrowserTestYN" ID="BrowserTestYN" VALUE=0> No
<INPUT type="button" value="Button" id="button1" name="button1 " 
onclick="isRadioChosen(document.form1.BrowserTestYN, 'Browser Requirement 
is a required field.')"> 
</FORM>

Alternatively, if you just want to make sure that they check one of the 
buttons you don't need any Javascript - just add a CHECKED attribute to 
one of the buttons and there will be no way to uncheck it without 
selecting another of the radios.

Phil

> I use the following code to validate an INPUT field:
> 
> <script language="javascript">
> ...
> if (form.URL.value == "") {
> 	alert("URL is a required field.");
> 	return false;
> 	}		
> 
> 
> 
> However, when I used the similiar code to validate if RADIO button field 
> is null, it did not work.  I used the following code.
> 
> In javascription section, 
> if (form.BrowserReqYN.value == "") {
> 	alert("Browser Requirement is a required field.");
> 	return false;
> 	}	
> 
> In HTML section
> <INPUT TYPE="radio" NAME="BrowserTestYN" VALUE=1> Yes  <br>
> <INPUT TYPE="radio" NAME="BrowserTestYN" VALUE=0> No
> 
> 
> Anyone know why this isn't working?  Is it because it's RADIO type?  
Thanks
> 
> -Bill K.
> 
> 

  Return to Index