Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript validation

Status
Not open for further replies.

SnakeEyes909

IS-IT--Management
Jun 17, 2008
26
US
Hello, I am new to javaScript, and I am working on an Online Form for my company. I have the form done, and now i am working on validation. I have a "main section" which i have the validation for. I also have a section that askes 6 yes/no questions. I need to validate that one of the radio box's for each question has been checked. The catch is, if the Yes box has been checked it "unhides" a section of questions that are required to be filled in. If the no box is checked no validation is needed other than that it-itself has been checked. Part of the code follows. each radio button set (yes/no) has its own name ie "rad1, rad2" I've been pounding my head for the last few days on this, any help would be greatly appreciated.

PS. the form works great, i have only included a sample as the whole code would be to big to post.

thanks.

<table width="927" border="0">
<tr>
<td width="554">Are you currently under the care of a Home Health Care Nurse* </td>
<td width="171" align="center" valign="middle">
<input type="radio" name="rad1" value="yes" onClick="showhide(this.value,'divtxta')">Yes
<input type="radio" name="rad1" value="no" onClick="showhide(this.value,'divtxta')">No
</td></tr></table>
<div id="divtxta" style="visibility:hidden;">
<table width=927 border=0 >
<tr>
<td height=5 colspan=3> Please provide the name and a contact number for your Home Health Agency (Leave blank if No.)</td>

</tr>
<tr>
<td width="28" height="25">&nbsp;</td>
<td width="567">Name*
<input name="HHAName" type="text" id="HHAName" size="60" /></td>
<td width="318">Phone*
<input name="HHAPhone" type="text" id="HHAPhone" size="30" /></td>
</tr>
</table>
</div>
<table width="927" border="0">
<tr>
<td width="565"><p>Has your home address changed since your last order?* </p></td>
<td width="171" align="center" valign="middle">
<input type="radio" name="rad2" value="yes" onClick="showhide(this.value,'divtxtb')">Yes
<input type="radio" name="rad2" value="no" onClick="showhide(this.value,'divtxtb')">No
</td></tr>
</table>
<div id="divtxtb" style="visibility:hidden;">
<table width="927" border="0">
<tr>
<td colspan="3">
<p>Please provide your new home address (Leave blank if No.) </p></td>
</tr>
<tr>
<td width="30">&nbsp;</td>
<td width="121">Address*</td>
<td width="762"><input name="NewAddress" type="text" size="75" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Address 2 </td>
<td><input name="Address2" type="text" size="75" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>City, State, Zip*</td>
<td><input name="City" type="text" />
<select name="NState" >
<option>OH</option>
<option>IN</option>
<option>MI</option>
<option selected> </option>
</select>
<input name="zip" type="text" /></td>
</tr>
</table>
</div>
 
This is the javascript that i have so far (minus my navagation stuff) this all works fine so far on the validation and hidding of items.


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>template ASP</title>


<script type="text/JavaScript">
<!--


function showhide(radval,divid)
{
if(radval=="no")
{document.getElementById(divid).style.visibility="Hidden"}
if(radval=="yes")
{
document.getElementById(divid).style.visibility="Visible"

}
}

<!-- script start
// Ensure that mandatory fields of
// form have been completed
function validateComplete(formObj)
{
if (emptyField(formObj.sFName))
alert("Please enter your First name.");
else if (emptyField(formObj.sLName))
alert("Please enter your Last name.");
else if (emptyField(formObj.phone))
alert("please enter a Phone Number.");
else if (emptyField(formObj.DayPhone))
alert("Please enter your Day Time Phone Number.");
else if (emptyField(formObj.sEmail))
alert("Please enter your E-mail address.");
else if (emptyField(formObj.Account))
alert("Please enter your Dasco account number.");
else if (emptyField(formObj.CEmail))
alert("Please confirm your E-mail address.");
else return true;

return false;
}

// Check to see if field is empty
function emptyField(textObj)
{
if (textObj.value.length == 0) return true;
for (var i=0; i<textObj.value.length; ++i) {
var ch = textObj.value.charAt(i);
if (ch != ' ' && ch != '\t') return false;
}
return true;
}

// script end -->


</script>
</head>
 
ok, guess this isn't the right place to get some help.

Thanks anyway.
 
Radio boxes are viewed as an array, so you'll have to check to see if rad1[0].checked==true and then evaluate the exposed inputs from there. You won't be able to use document.getElementById because that only returns one element. Use something like document.forms['form name here'].elements['rad1'][0].checked.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top