...

View Full Version : form validation not working



chelvis
07-26-2002, 05:52 PM
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>

Roy Sinclair
07-26-2002, 09:01 PM
<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum