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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

validating "other" field

Status
Not open for further replies.

bigbird3156

Programmer
Feb 20, 2001
183
AU
Hi I am creating a form for donations to a charitable organisation - I have created a radio group of $ amounts that can be selected. the last radio button is an "other" field that then has a text box for the person to enter their own donation amount.

I have a validation script to check that one of these fields has been selected but does anyone have any ideas on how to create a script that will make the donations text field a required field only when the "other" radio button is selected ???

would apreciate you help on this one!!!

[wiggle]The Bird from Down Under- Bigbird 3156
Programmer?? - I thought the option was pretender not programmer!![jester]
 
Here's an example, I've left other parts of the validation out. However, this should answer your question:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function validate(frm) {
   //find out if the "other" element is checked
   if (frm.elements["donation"][2].checked) {
      //if it is checked, make sure there's a value in the text field
      if (frm.elements["donationOther"].value = "") {
         alert("You must specify an amount if the 'Other' box is checked");
         return false;
      }
   }
   alert("Validation Successful");
   return false;
}

</script>
<style type="text/css"></style>
</head>
<body>
<form onsubmit="return validate(this)">
   <input type="radio" value="100" name="donation"> 100 <br />
   <input type="radio" value="200" name="donation"> 200 <br />
   <input type="radio" value="" name="donation"> Other <input type="text" name="donationOther" />
   <br /><br />
   <input type="submit" value="validate me" />
</form>
</body>
</html>

-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
thanks kaht, but it did not work for me... it could be that I have done something wrong... i am only very new to js and am using this job as a bit of a learning experience...

can you help - everything comes up as validation sucessful...

here are the relivant extracts:
JS...

function Validateotherdon(donationfrm)
{
//find out if the other" element is checked
if (donationfrm.DonationValue[5].checked) {
//if it is checked, make sure there's a value in the text field
if (donationfrm.otherdonation.value ="") {
alert("You must specify an amount if the 'Other' box is checked");
return false;
}
}
alert("Validation Successful");
return false;
}
HTML...

<strong>I would like to donate the amount of<font color="#FF0000" size="5">*</font>:</strong><br>
<label>
<input name="DonationValue" type="radio" value="25.00">
$25.00</label>
<br>
<label>
<input type="radio" name="DonationValue" value="50.00">
$50.00</label>
<br>
<label>
<input type="radio" name="DonationValue" value="100.00">
$100.00</label>
<br>
<label>
<input type="radio" name="DonationValue" value="250.00">
$250.00</label>
<br>
<label>
<input type="radio" name="DonationValue" value="500.00">
$500.00</label>
<br>
<label>
<input type="radio" name="DonationValue" value="Other">
Other Amount </label>
$
<input name="otherdonation" type="text" id="other donation" value="">
<br>


[wiggle]The Bird from Down Under- Bigbird 3156
Programmer?? - I thought the option was pretender not programmer!![jester]
 
[1] major
[1.1]
>if (donationfrm.otherdonation.value ="") {
[tt]if (donationfrm.otherdonation.value =[red]=[/red]"") {[/tt]
[1.2]
> alert("Validation Successful");
return false;

[tt] alert("Validation Successful");
return [red]true[/red];[/tt]

[2] minor: if you really mean it, otherwise I would guess you need to watch detail. But there is no harm made for the lines posted.
><input name="otherdonation" type="text" id="other donation" value="">
[tt]<input name="otherdonation" type="text" id="othe[highlight]rd[/highlight]onation" value="">[/tt]
 
thanks tsuji,

I just sorted out the problem myself and came online to tell you not to bother - & there was the solution -

thanks heaps for all your help - i'm sure i'll get there one day!!!

[wiggle]The Bird from Down Under- Bigbird 3156
Programmer?? - I thought the option was pretender not programmer!![jester]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top