Click to See Complete Forum and Search --> : Validating Radio Buttons In A Form


96turnerri
11-21-2003, 12:35 PM
hi guys its me again 96 lol

i have this form that i wish to validate, i validated the textboxes but some of the form parts a radio buttons heres one of the radio buttons code snippits

heres the code im using to validate the textboxes

<script type="text/javascript">
function validate() {
if (document.quotation.Name.value.length < 5) {
alert("Please Enter Your Full Name.");
return false;
}
if (document.quotation.Email.value.length < 10) {
alert("Please Enter Your Full Email Address.");
return false;
}
return true;
}
</script>

and the code for the radio button

Do You Require A Domain Name?<br>
<input type="radio" name="Domain" value="Yes">Yes i.e. <input type="text" value="http://www." size="25" name="DomainName"><br>
<input type="radio" name="Domain" value="No">No

what i would like it to do onsubmit it runs validate(), so when a user clicks submit, i need it to validate they have to select one of the radio buttons, and if they selected yes, i need a example to be entered

i have tried a couple of methods but i cant get radio buttons to validate

Thanks
96

gil davis
11-21-2003, 12:51 PM
I would suggest that you have the "no" button declared "checked". That way you just have to look at the "yes" button and then check the domain name field.

...
if (document.Quotation.Domain[0].checked)
{if (document.Quotation.DomainName.value.length < whatever)
{// alert and focus}
}
...
<input type="radio" name="Domain" value="Yes">Yes i.e. <input type="text" value="" size="25" name="DomainName"><br>
<input type="radio" name="Domain" value="No" checked>No

96turnerri
11-21-2003, 01:01 PM
thanks for your help, i have made no checked as you said, i still cant get it to work so heres my entires validating code

<script type="text/javascript">
function validate() {
if (document.quotation.Name.value.length < 5) {
alert("Please Enter Your Full Name.");
document.quotation.Name.focus();
return false;
}
if (document.quotation.Email.value.length < 10) {
alert("Please Enter Your Full Email Address.");
document.quotation.Email.focus();
return false;
}
if (document.Quotation.Domain[0].checked)
{if (document.Quotation.DomainName.value.length < 20)
{alert("Please Enter An Example.");
document.quotation.DomainName.focus();
return false;
}
return true;
}
</script>

gil davis
11-21-2003, 01:03 PM
You have "Quotation" and "quotation" specified in your script. Which is correct? Object names are CaSeSeNsItIvE.

96turnerri
11-21-2003, 01:06 PM
quotation is correct but it still not working :(

gil davis
11-21-2003, 01:12 PM
Please post the form's HTML.

96turnerri
11-21-2003, 01:13 PM
here is the forms html, thanks again

<form name="quotation" onSubmit="return validate();" method="post" action="http://home.btconnect.com/cgi-bin/mailto/rich@tturner88.freeserve.co.uk">
<strong>Contact Information: (* = optional)</strong><br><br>
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="Name" size="30"></td>
</tr>
<tr>
<td>Address*</td>
<td><textarea rows="5" name="Address" cols="25"><--ADDRESS FINDER / CHECKER--></textarea></td>
</tr>
<tr>
<td>Telephone*</td>
<td><input type="text" size="4" name="Prefix"> <input type="text" size="6" name="STD"> <input type="text" size="10" name="Number"> <a href="telephone.htm" onclick="window.open( this.href, '', 'height=210,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Telephone Number Help'; return true;">Help?</a></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="30"></td>
</tr>
</table><br>
<strong>Website Information:</strong><br><br>
Do You Require A Domain Name? <a href="domainname.htm" onclick="window.open( this.href, '', 'height=250,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Domain Name Help'; return true;">Help?</a><br>
<input type="radio" name="Domain" value="Yes">Yes i.e. <input type="text" value="http://www." size="25" name="DomainName"> <--DOMAIN NAME CHECKER--><br>
<input type="radio" name="Domain" value="No" checked>No<br><br>
Do You Require Webhosting? <a href="webhosting.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Web Hosting Help'; return true;">Help?</a><br>
<input type="radio" name="Hosting" value="Yes">Yes<br>
<input type="radio" name="Hosting" value="No">No<br><br>
Do You Already Have An Existing Website?<br>
<input type="radio" name="ExistingSite" value="Yes">Yes, It Just Needs A Revamp, The Current Site Is <input type="text" value="http://www." size="25" name="DomainName"><br>
<input type="radio" name="ExistingSite" value="No">No, I Need One Built From Scratch<br><br>
What Is The Purpose Of This Site? <a href="purpose.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Site Purpose Help'; return true;">Help?</a><br>
<table border="0">
<tr>
<td><input type="radio" name="SitePurpose" value="Company"> Company</td>
<td><input type="radio" name="SitePurpose" value="Informative"> Informative</td>
</tr>
<tr>
<td><input type="radio" name="SitePurpose" value="Interest"> Interest/Hobby</td>
<td><input type="radio" name="SitePurpose" value="Personal"> Personal</td>
</tr>
</table><br>
Will Your Site Need Updating Regularly?<br>
<input type="radio" name="Updates" value="Yes">Yes, If So How Often? <select name="Update"><option> </option><option>Weekly</option><option>Monthly</option><option>Yearly</option></select><br>
<input type="radio" name="Updates" value="No">No<br><br>
Approximatly, How Many Pages Are Needed For Your Site?<br><select name="NumberPages"><option>1-4</option><option>5-8</option><option>9-12</option><option>13-16</option><option>17-20</option><option>Other</option></select> Other <input type="text" name="PageNumberOther" size="3"><br><br>
Do You Already Have A Company Logo & Colours? <a href="logocolours.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Company Logo & Colours Help'; return true;">Help?</a><br>
<input type="radio" name="LogoColour" value="Yes">Yes<br>
<input type="radio" name="LogoColour" value="No">No, Do You Require Us To Create A Logo & Colour Design <select><option> </option><option>Yes Both</option><option>Logo</option><option>Colours</option><option>No</option></select><br><br>
What Features Do You Require In Your Site? <a href="features.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Feature Help'; return true;">Help?</a><br>
<input type="checkbox" name="FlashMenu"> Flash Menu<br>
<input type="checkbox" name="Forms"> Online Forms, How Many <select name="forms"><option> </option><option>1</option><option>2</option><option>3</option><option>4+</option></select><br>
<input type="checkbox" name="ETC"> ETC<br><br>
Are There Any Non-Listed Features That You Require Or Have Seen Before?, If So Please Write Them Here With Example <a href="notlisted.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Non-Listed Feature Help'; return true;">Help?</a><br>
Feature 1 <input type="text" name="FeatureNL1" size="15"> Example Of Feature 1 <input type="text" name="FeatureNL1URL" value="http://www." size="25"><br>
Feature 2 <input type="text" name="FeatureNL2" size="15"> Example Of Feature 2 <input type="text" name="FeatureNL2URL" value="http://www." size="25"><br>
Feature 3 <input type="text" name="FeatureNL3" size="15"> Example Of Feature 3 <input type="text" name="FeatureNL3URL" value="http://www." size="25"><br><br>
<input type="submit" value="Submit"> <input type="reset" value="Clear Form">
</form>
</div>
</div>
<!--end content -->
<div id="navBar">
<div id="search">
<form action="#">
<label>search</label>
<input name="searchFor" type="text" size="10">
<input name="goButton" type="submit" value="go">
</form>

gil davis
11-21-2003, 01:24 PM
You have more than one textbox called "DomainName". That makes an array just like the checkboxes.

96turnerri
11-21-2003, 01:28 PM
ok now it works thanks, except that when you select yes to having a domain name a prompt box comes up saying please enter an example when you click ok instead of focusing it just submits form and doesnt focus :confused:

gil davis
11-21-2003, 01:31 PM
That means you still have errors.

Do you use IE? Do you see the yellow triangle in the lower left corner? Click it and it will tell you what line contains the error.

gil davis
11-21-2003, 01:35 PM
You might not see the triangle because of the thank you page.

Check your "}".

96turnerri
11-21-2003, 01:37 PM
yes i am previewing in ie and i dont get no triangle in the corner only after i press ok just for about 0.5seconds whilst page is submitting after clicking ok on please enter an example

96turnerri
11-21-2003, 01:41 PM
i just managed to catch the triangle it says document.quotation.DomainName is null or not an object

gil davis
11-21-2003, 01:47 PM
Does the form still have two objects called "DomainName"? Are you looking at the right one with the script?

96turnerri
11-21-2003, 01:51 PM
yes i changed the other one tosomething else and its still shows an error :confused:

gil davis
11-21-2003, 01:55 PM
You really aren't helping me much.

The error tells you that you have tried to access an object that does not exist. You must make sure that the object name is spelled and capitalized correctly in the script. It has to match the object name as specified in the HTML.

In order to give you more specific help, I need to see the script and the form as it now exists.

96turnerri
11-21-2003, 02:05 PM
sorry heres the code as it is now, document.quotation.DomainName is not a null objec tit exists

heres the new code

<script type="text/javascript">
function validate() {
if (document.quotation.Name.value.length < 5) {
alert("Please Enter Your Full Name.");
document.quotation.Name.focus();
return false;
}
if (document.quotation.Email.value.length < 10) {
alert("Please Enter Your Full Email Address.");
document.quotation.Email.focus();
return false;
}
if (document.quotation.Domain[0].checked) {
if (document.quotation.DomainName.value.length < 20);
alert("Please Enter An Example Of A Domain Name.");
document.quotation.DomianName.focus();
return false;
}
return true;
}
</script>

and the form code

<form name="quotation" onSubmit="return validate();" method="post" action="http://home.btconnect.com/cgi-bin/mailto/rich@tturner88.freeserve.co.uk">
<strong>Contact Information: (* = optional)</strong><br><br>
<table border="0">
<tr>
<td>Name</td>
<td><input type="text" name="Name" size="30"></td>
</tr>
<tr>
<td>Address*</td>
<td><textarea rows="5" name="Address" cols="25"><--ADDRESS FINDER / CHECKER--></textarea></td>
</tr>
<tr>
<td>Telephone*</td>
<td><input type="text" size="4" name="Prefix"> <input type="text" size="6" name="STD"> <input type="text" size="10" name="Number"> <a href="telephone.htm" onclick="window.open( this.href, '', 'height=210,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Telephone Number Help'; return true;">Help?</a></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="Email" size="30"></td>
</tr>
</table><br>
<strong>Website Information:</strong><br><br>
Do You Require A Domain Name? <a href="domainname.htm" onclick="window.open( this.href, '', 'height=250,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Domain Name Help'; return true;">Help?</a><br>
<input type="radio" name="Domain" value="Yes">Yes i.e. <input type="text" value="http://www." size="25" name="DomainName"> <--DOMAIN NAME CHECKER--><br>
<input type="radio" name="Domain" value="No" checked>No<br><br>
Do You Require Webhosting? <a href="webhosting.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Web Hosting Help'; return true;">Help?</a><br>
<input type="radio" name="Hosting" value="Yes">Yes<br>
<input type="radio" name="Hosting" value="No">No<br><br>
Do You Already Have An Existing Website?<br>
<input type="radio" name="ExistingSite" value="Yes">Yes, It Just Needs A Revamp, The Current Site Is <input type="text" value="http://www." size="25" name="ExistingURL"><br>
<input type="radio" name="ExistingSite" value="No">No, I Need One Built From Scratch<br><br>
What Is The Purpose Of This Site? <a href="purpose.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Site Purpose Help'; return true;">Help?</a><br>
<table border="0">
<tr>
<td><input type="radio" name="SitePurpose" value="Company"> Company</td>
<td><input type="radio" name="SitePurpose" value="Informative"> Informative</td>
</tr>
<tr>
<td><input type="radio" name="SitePurpose" value="Interest"> Interest/Hobby</td>
<td><input type="radio" name="SitePurpose" value="Personal"> Personal</td>
</tr>
</table><br>
Will Your Site Need Updating Regularly?<br>
<input type="radio" name="Updates" value="Yes">Yes, If So How Often? <select name="Update"><option> </option><option>Weekly</option><option>Monthly</option><option>Yearly</option></select><br>
<input type="radio" name="Updates" value="No">No<br><br>
Approximatly, How Many Pages Are Needed For Your Site?<br><select name="NumberPages"><option>1-4</option><option>5-8</option><option>9-12</option><option>13-16</option><option>17-20</option><option>Other</option></select> Other <input type="text" name="PageNumberOther" size="3"><br><br>
Do You Already Have A Company Logo & Colours? <a href="logocolours.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Company Logo & Colours Help'; return true;">Help?</a><br>
<input type="radio" name="LogoColour" value="Yes">Yes<br>
<input type="radio" name="LogoColour" value="No">No, Do You Require Us To Create A Logo & Colour Design <select><option> </option><option>Yes Both</option><option>Logo</option><option>Colours</option><option>No</option></select><br><br>
What Features Do You Require In Your Site? <a href="features.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Feature Help'; return true;">Help?</a><br>
<input type="checkbox" name="FlashMenu"> Flash Menu<br>
<input type="checkbox" name="Forms"> Online Forms, How Many <select name="forms"><option> </option><option>1</option><option>2</option><option>3</option><option>4+</option></select><br>
<input type="checkbox" name="ETC"> ETC<br><br>
Are There Any Non-Listed Features That You Require Or Have Seen Before?, If So Please Write Them Here With Example <a href="notlisted.htm" onclick="window.open( this.href, '', 'height=200,width=300,scrollbars=no' );return false;" onMouseOver="window.status='Non-Listed Feature Help'; return true;">Help?</a><br>
Feature 1 <input type="text" name="FeatureNL1" size="15"> Example Of Feature 1 <input type="text" name="FeatureNL1URL" value="http://www." size="25"><br>
Feature 2 <input type="text" name="FeatureNL2" size="15"> Example Of Feature 2 <input type="text" name="FeatureNL2URL" value="http://www." size="25"><br>
Feature 3 <input type="text" name="FeatureNL3" size="15"> Example Of Feature 3 <input type="text" name="FeatureNL3URL" value="http://www." size="25"><br><br>
<input type="submit" value="Submit"> <input type="reset" value="Clear Form">
</form>

gil davis
11-21-2003, 02:22 PM
(heavy sigh)
Originally posted by 96turnerri
document.quotation.DomianName.focus();

96turnerri
11-21-2003, 02:35 PM
god i checked and doubled checked that :rolleyes: must be all these late nites ive been having lol thanks for all your help

aprreciated
Rich