Click to See Complete Forum and Search --> : Problems with validation


Dman87
01-04-2007, 10:50 AM
Hello all dident know if this was the wright place to post this if its not just move it. Im having some problem with the validation function (yes im new to javascript) my page wont display what i want it to. and also my clear all button dosent work now eather. and i think the problem is the cost bit of the code i must be doing it wrong, when the user selects and option eg all inclusive and he/she is over 60 they get 50% off, thats how its suppost to work.

function validate code

code:
<html>
<head>
<script type = "text/javascript">
function blanks()
{form1.reset()}

function validate()
{
title = form1.s1.options[form1.s1.selectedIndex].text
forename = form1.forename.value
surname = form1.surname.value
street = form1.street.value
town = form1.town.value
postcode = form1.postcode.value
email = form1.email.value
age = form1.age.value
type = form1.s2.options[form1.s2.selectedIndex].text
if ((age=="") || (forename=="") || (surname=="") || (title=="Pick") || (street=="") || (town=="") || (postcode=="") || (email=="") || (type=="pick"))
{
alert("Missing Details")
}
else
{
if (parseInt(age)<18)
{
alert("You must be over 18 to register")
}
else
{
cost = 0
"All inclusive" = 500.00
"Waterskiing only" = 200.00
"Canoeing only" = 100.00
"Fishing only" = 300.00
ages = parseInt(age)
if (ages >= 60)
{
cost = "All inclusive" - ("All inclusive"*0.50)
cost = "Waterskiing only" - ("Waterskiing only"*0.50)
cost = "Canoeing only" - ("Canoeing only"*0.50)
cost = "Fishing only" - ("Fishing only"*0.50)
}
if ((ages >= 45) && (ages < 60))
{
cost = "All inclusive" - ("All inclusive"*0.25)
cost = "Waterskiing only" - ("Waterskiing only"*0.25)
cost = "Canoeing only" - ("Canoeing only"*0.25)
cost = "Fishing only" - ("Fishing only"*0.25)
}
if ((ages >=18) && (ages<40))
{
cost = "All inclusive"
cost = "Waterskiing only"
cost = "Canoeing only"
cost = "Fishing only"
}
alert(forename.substring(0,1).toUpperCase()+". "+surname+"\n"+type.toUpperCase()+"\nCost = £"+cost.toFixed(2))
}
}
}
function getName()
{
var Name = new Array(3)
var Email = new Array(3)
for (i=0;i<Name.length;i++)
{
Name[i] = prompt("Please enter Name ","")
Email[i] = prompt("Please enter Email ","")
}
document.write("The following people will recieve a message: "+"<br>")
for (i=0;i<Name.length;i++)
{
document.write("Name : "+Name[i])
document.write(" Email : "+Email[i]+"<br>")
}
}
code:

And the option

code:
<p>
Membership Type
<select name="select" id="s2">
<option value="Pick type">Pick</option>
<option value="£500.00">All inclusive</option>
<option value="£200.00">Waterskiing only</option>
<option value="£100.00">Canoeing only</option>
<option value="£300.00">Fishing only</option>
</select>
</p>
code:

as i say im new to javascript and new to forums, be nice, and if you can help that would be a plus.

A1ien51
01-04-2007, 10:54 AM
"All inclusive" - ("All inclusive"*0.25)


those are strings and not variables.

Are you after the selected value?

Eric

Dman87
01-04-2007, 11:23 AM
im not quite sure,

<option value="£500.00">All inclusive</option>
<option value="£200.00">Waterskiing only</option>
<option value="£100.00">Canoeing only</option>
<option value="£300.00">Fishing only</option>

all inclusive is suppost to cost £500.00 and if im over 60 i get 50% off, how do i code this so that if i pick waterskiiing only and im over 60 i also get 50% off etc, and if im under 45 it will display normal price £500.00.

it is also ment to pop up in a dialog box when i click send, which will display name address etc it will also display the cost and the membership type i chooice.

A1ien51
01-04-2007, 11:31 AM
"All inclusive" = 500.00
"Waterskiing only" = 200.00
"Canoeing only" = 100.00
"Fishing only" = 300.00

you can not make strings into variables
it would have to be something like

var allInclusive = 500;

Trailing zeros will be chopped off. JavaScript does not hold them.

cost = "All inclusive" - ("All inclusive"*0.50)
cost = "Waterskiing only" - ("Waterskiing only"*0.50)
cost = "Canoeing only" - ("Canoeing only"*0.50)
cost = "Fishing only" - ("Fishing only"*0.50)

when you are doing this you are replceing the variable cost in every line.

Are you trying to alter the values of the select element after they pick the age. Are you going to rely on this to do the actual cost because I would like to know so I can make it free for myself!

Eric

Dman87
01-04-2007, 12:27 PM
hmm i think ill have to read up on turning varibals into strings etc for javascript. iv tryed it like this, dident like it.

else
{
cost = 0
var All inclusive = 500;
var Waterskiing only = 200;
var Canoeing only = 100;
var Fishing only = 300;
ages = parseInt(age)
if (ages >= 60)
{
cost = var All inclusive - (var All inclusive*0.50)
cost = var Waterskiing only - (var Waterskiing only*0.50)
cost = var Canoeing only - (var Canoeing only*0.50)
cost = var Fishing only - (var Fishing only*0.50)
}
if ((ages >= 45) && (ages < 60))
{
cost = var All inclusive - (var All inclusive*0.25)
cost = var Waterskiing only - (var Waterskiing only*0.25)
cost = var Canoeing only - (var Canoeing only*0.25)
cost = var Fishing only - (var Fishing only*0.25)
}
if ((ages >=18) && (ages<40))
{
cost = var All inclusive
cost = var Waterskiing only
cost = var Canoeing only
cost = var Fishing only
}
alert(forename.substring(0,1).toUpperCase()+". "+surname+"\n"+type.toUpperCase()+"\nCost = £"+cost.toFixed(2))


Should i be doing the calculating diffrenty? not like this

cost = 0
var All inclusive = 500;
var Waterskiing only = 200;
var Canoeing only = 100;
var Fishing only = 300;
ages = parseInt(age)
if (ages >= 60)
{
cost = var All inclusive - (var All inclusive*0.50)
cost = var Waterskiing only - (var Waterskiing only*0.50)
cost = var Canoeing only - (var Canoeing only*0.50)
cost = var Fishing only - (var Fishing only*0.50)

A1ien51
01-04-2007, 12:43 PM
variables can not have spaces in them.

Dman87
01-04-2007, 01:03 PM
ok, same post without spaces. i have also notices that appart from spaces it dosent like capitals, only in certan places could that be part of the problem?

Or dose anyone have any links to tutorials of calculating cost using the options object.

Dman87
01-05-2007, 01:04 PM
any extra help would be aprichiated.