...

View Full Version : Form Validation Help



Stoffel
01-19-2003, 02:19 PM
Hiya,

It may be a stupid question, but I am kind of new in JavaScript.

I have a form, and in that form, I want to calculate the price of a movie.
First you have to choose between VHS or DVD and then between the number you want to buy of the movie.

How do I make a script that does that, but it may only be activated when a movie is chosen.

Here is a part of the code:


<form>
<input type="text" name="moviename" value="" size="30" maxlength="100" disabled>
<a href="#" onclick="window.open('pop1.html','','top=100,left=100,width=350,height=400'); return false;">Choose your movie</a>
<select name="type">
<option value="vhs">VHS</option>
<option value="dvd">DVD</option>
</select>
<select name="how_many">
<option value="1">1</option>
<option value="2">2</option <option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="text" name="price" value="" size="10">
</form>


The price of the movie can be put in the popup.

I hope you understand what I'm meaning, and that you can help me

See ya

whammy
01-19-2003, 06:52 PM
Something like this?



<html>
<head>
<title>Get the price</title>
<script type="text/javascript">
<!--
function formatCurrency(num) {
num = num.toString().replace(/\,/g,"");
num = !isNaN(num) ? Math.round(num * 100) / 100 : 0;
num.toString().indexOf(".") == -1 ? num += ".00" : void 0;
while(/\.\d{0,1}$/.test(num)) num += "0";
var objRegExp = new RegExp('(-?\[0-9]+)([0-9]{3})');
while(objRegExp.test(num)) num = num.toString().replace(objRegExp,'$1,$2');
return num;
}
function calculatePrice(movieType,movieQuantity) {
var moviePrice = new Array(0,9.95,14.95)
if(movieType > 0 && movieQuantity != "") {
document.forms[0].price.value = "$" + formatCurrency(moviePrice[movieType] * movieQuantity);
// window.open('pop1.html','','top=100,left=100,width=350,height=400')
}
else {
document.forms[0].price.value = "";
}
}
// -->
</script>
</head>
<body>
<form id="form1" action="javascript://" onsubmit="return false">
<table>
<tr>
<td>Movie:</td>
<td><input type="text" name="moviename" value="Enemy Mine" size="30" onfocus="this.blur()" /></td>
</tr>
<tr>
<td>Format:</td>
<td>
<select name="movietype" onchange="calculatePrice(this.form.movietype.selectedIndex,this.form.how_many.options[this.form.how_many.selectedIndex].value)">
<option value="">------</option>
<option value="vhs">VHS</option>
<option value="dvd">DVD</option>
</select>
</td>
</tr>
<tr>
<td>Quantity:</td>
<td>
<select name="how_many" onchange="calculatePrice(this.form.movietype.selectedIndex,this.form.how_many.options[this.form.how_many.selectedIndex].value)">
<option value="">--</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Price:</td>
<td>
<input type="text" name="price" value="" size="10" onfocus="this.blur()" />
</td>
</tr>
</table>
</form>
</body>
</html>


Hope this helps. :)

Stoffel
01-19-2003, 07:16 PM
Thnx a lot

I'll take a look at it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum