Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-26-2002, 04:52 PM   PM User | #1
chelvis
Regular Coder

 
Join Date: Jun 2002
Posts: 406
Thanks: 0
Thanked 0 Times in 0 Posts
chelvis is an unknown quantity at this point
form validation not working

I created a form and validation, but it is not working. Actually the form has 2 radio buttons (Initiall none of them are selected). So I wrote a validation that user should select a radio button before submitting (I am using an image instead of submit button). Also I have to validate a text box to see the user enters some thing.

When I was testing, if the user doesn't enter any thing in the radio buton, it shows an alert. When I click on the alert, I get a blank white page with "false" on it. Also even if I select a radio button, and leave the text box blank (validation for text box) it comes and says "Please select a radio button". I couldn't figure this out. Some one help me

<html>
<head>
<title>blank</title>
<script language="JavaScript">

function validate()
{

var radio_choice = false;

for (counter = 0; counter < userinput.radio_db; counter++)
{
if (userinput.radio_db[counter].checked)
radio_choice = true;
}
if (!radio_choice)
{
alert("Please select a database.")
return false;
}
if ((document.userinput.for1.value.length == 0) || (document.userinput.for1.value = null)){
alert ('Please enter some search term in the text box');
userinput.for1.focus();
}
return true;
}
</script>


</head>

<body bgcolor="#FFFFFF" topmargin="0" marginheight="0" marginwidth="0">

<form name="userinput" action="#" method="post">
<table>
<tr>
<td valign="top" bgcolor="#EFEFD6"><B>SELECT DATABASE</b></td>
</tr>
<tr>
<td valign="top">
<input type="radio" name="radio_db" value="lit"> Lit &nbsp; &nbsp; <input type="radio" name="radio_db" value="pat">Pat </td>
</tr>
<tr>
</tr>
<tr>
<td valign="top" bgcolor="#EFEFD6"><b>SEARCH FOR</b></td>
</tr>
<tr>
<td valign="top" align="right" bgcolor="#EFEFD6" ><input type="text" name="for1" size="29"></td></tr>
<tr><td valign="top"><A href="javascript:validate();"><img src="images/search_orange.gif" border="0"></a></td></tr>
</table>
</form>
</body>
</html>
chelvis is offline   Reply With Quote
Old 07-26-2002, 08:01 PM   PM User | #2
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
Code:
<html> 
<head> 
<title>blank</title> 
<script language="JavaScript"> 

function validate() 
	{ 
	var radio_choice = false; 
	for (counter = 0; counter < document.userinput.radio_db.length; counter++) 
		{ 
		if (document.userinput.radio_db[counter].checked) 
			radio_choice = true; 
		} 
	if (!radio_choice) 
		{ 
		alert("Please select a database.") 
		return false; 
		} 
	if ((document.userinput.for1.value.length == 0) || (document.userinput.for1.value = null))
		{ 
		alert ('Please enter some search term in the text box'); 
		userinput.for1.focus(); 
		return false;
		} 
	document.userinput.submit(); 
	} 
</script> 


</head> 

<body bgcolor="#FFFFFF" topmargin="0" marginheight="0" marginwidth="0"> 

<form name="userinput" action="#" method="post"> 
<table> 
<tr> 
<td valign="top" bgcolor="#EFEFD6"><B>SELECT DATABASE</b></td> 
</tr> 
<tr> 
<td valign="top"> 
<input type="radio" name="radio_db" value="lit"> Lit     <input type="radio" name="radio_db" value="pat">Pat </td> 
</tr> 
<tr> 
</tr> 
<tr> 
<td valign="top" bgcolor="#EFEFD6"><b>SEARCH FOR</b></td> 
</tr> 
<tr> 
<td valign="top" align="right" bgcolor="#EFEFD6" ><input type="text" name="for1" size="29"></td></tr> 
<tr><td valign="top"><A href="#" onclick="validate();return false;">Submit</a></td></tr> 
</table> 
</form> 
</body> 
</html>
Roy Sinclair is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

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

Forum Jump


All times are GMT +1. The time now is 10:38 AM.


Advertisement
Log in to turn off these ads.