View Full Version : validate form

07-15-2008, 04:42 AM
Hi everyone, i have to validate a form for a client, i have it working apart from two sections.

Do you require transportation? yes or no
Do you require installation? yes or no

when clicked yes a new div appears "divtxta", i need this div to be validated when yes is checked but not to be validated when no is checked.

If anyone can help i would really appreciate it.


<!-- End DI Tracking Code -->
<script language="JavaScript">
function showhide(inval,divid)
{document.getElementById(divid).style.visibility=" visible"}
document.getElementById(divid).style.visibility="h idden"
document.contact.txta.value="Enter your reason here"

function showhide(tranval,divid)
{document.getElementById(divid).style.visibility=" visible"}
document.getElementById(divid).style.visibility="h idden"
document.contact.txta.value="Enter your reason here"



<div id="container">
<div id="wrapper">

<div id="header">
<!--[if IE]>
#header {width: 960px;}
<img src="images/bottom-banner.gif" />
<!-- <img src="images/site-shed-sales.jpg" width="117" height="83" /> -->

<div id="nav">

<div id="left">
<h1 class="normal">Contact Site Shed Sales</h1>
<br />
To enquire about our products or services, please submit your details below and we will contact you shortly.
<br />
<br />
<form name="contact" method="post" action="mail.php">

<input type="text size="40" name="name" />
<br />

<label>Company / Organisation</label>
<input type="text size="40" name="company" />
<br />

<input type="text size="40" name="phone" />
<br />

<input type="text size="40" name="email" />
<br />

<label>Building Interested in</label>
<textarea rows="1" cols="40" name="interest"><?=$building?></textarea>
<br />

<label>Do you require transportation?</label>
<input type="radio" name="tran" value="yes" onClick="showhide(this.value,'divtxta')">Yes
<input type="radio" name="tran" value="no" onClick="showhide(this.value,'divtxta')">No
<br />
<div id="divtxta" style="visibility:hidden;">
<textarea name="location" rows="3" cols="40">Please enter location</textarea>

<label>Do you require installation?</label>
<input type="radio" name="in" value="yes" onClick="showhide(this.value,'divtxtatwo')">Yes
<input type="radio" name="in" value="no" onClick="showhide(this.value,'divtxtatwo')">No
<br />
<div id="divtxtatwo" style="visibility:hidden;">
<textarea name="sitedescription" rows="3" cols="40">Please enter site description</textarea>

<label>Any other information</label>
<textarea rows="10" cols="40" name="additional"></textarea>
<br />
<br />

<input type="submit" name="submit" value="Send Enquiry" /><br />

<div id='contact_errorloc' class='error_strings'></div>
<br />
<br />

<script language="JavaScript" type="text/javascript">
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("contact");

frmvalidator.addValidation("name","req","Please enter your Name.");
frmvalidator.addValidation("name","maxlen=20", "Max length for Name is 20");
frmvalidator.addValidation("name","alpha_s","Name can contain alphabetic chars only");

frmvalidator.addValidation("company","req","Please enter your Company / Organisation.");
frmvalidator.addValidation("company","maxlen=20"," For LastName, Max length is 20");



frmvalidator.addValidation("interest","Please enter the building interested in. ");
frmvalidator.addValidation("interest","maxlen=40") ;

frmvalidator.addValidation("location","req","Pleas e enter the site location. ");
frmvalidator.addValidation("location","maxlen=40") ;

frmvalidator.addValidation("sitedescription","req" ,"Please enter the site description. ");
frmvalidator.addValidation("sitedescription","maxl en=40");



I am also using a JavaScript to validate the form "gen_validatorv31.js"
that i got from http://www.javascript-coder.com/html...lidation.phtml

Many Thanks

07-15-2008, 09:05 AM
Hi Darren,

why not use an onsubmit="extra_validation(contact)" in the form tag, then in this function check each radio set in turn to see if set to yes, if so, add in frmvalidator.addValidation("location","maxlen=250");

Ok nevermind this I've just read the tutorial on your validation script. I'm guessing you haven't read it right the way through because it answers your question. Check this out: http://www.javascript-coder.com/html-form/javascript-form-validation.phtml#custom