Javascript help needed

Associate
Joined
21 Mar 2007
Posts
41
Hi,

I'm rather new to javascript and am trying to validate a form and then mail the results. I've got the validator working, but it refuses to attempt to open the php file with the information to create and send the email.

<script type="text/javascript">
<!--
function validateForm()
{
var themessage = "You missed something:"
var valid = true
if (document.volunteers.name.value=="")
{
alert (themessage + " Enter Name")
valid = false;
}
if (document.volunteers.address.value == "")
{
alert (themessage + "\nPlease enter line 1 of your address")
valid = false;
}
if (document.volunteers.address2.value == "")
{
alert (themessage + "\nPlease enter line 2 of your address")
valid = false;
}
if (document.volunteers.postcode.value == "")
{
alert (themessage + "\nPlease enter your postcode")
valid = false
}
if (document.volunteers.telephone.value == "")
{
alert (themessage + "\nPlease enter your telephone number")
valid = false;
}
if (document.volunteers.email.value == "")
{
alert (themessage + "\nPlease enter your email address")
valid = false;
}
if(document.volunteers.email.value!=document.volunteers.email2.value)
{
alert (themessage + "\nYour email confirmation does not match")
valid = false;
}
if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.volunteers.email.value))
{
alert (themessage + "\nYour e-mail address contains invalid characters")
valid = false;
}
if (document.volunteers.birthday.value == "")
{
alert (themessage + "\nPlease enter your day of birth")
valid = false;
}
if (document.volunteers.birthmonth.value == "")
{
alert (themessage + "\nPlease enter your birth month")
valid = false;
}
if(document.volunteers.birthyear.value == "")
{
alert (themessage + "\nPlease enter the year you were born")
valid = false;
}
if (document.volunteers.ability.value == "")
{
alert (themessage + "\nPlease enter your ability level")
valid = false;
}
var radioCheck = false;
for (i = 0; i < document.volunteers.preference.length; i++)
{
if (document.volunteers.preference.checked)
radioCheck = true;
}
if (!radioCheck)
{
alert (themessage + "\nPlease enter your preferred method of communication")
valid = false;
}
var radioCheck = false;
for (i = 0; i < document.volunteers.internet.length; i++)
{
if (document.volunteers.internet.checked)
radioCheck = true;
}
if (!radioCheck)
{
alert (themessage + "\nPlease enter whether you have regular access to the internet")
valid = false;
}
var radioCheck = false;
for (i = 0; i < document.volunteers.permission.length; i++)
{
if (document.volunteers.permission.checked)
radioCheck = true;
}
if (!radioCheck)
{
alert (themessage + "\nYou must give your, or your parent/guardian's, permission for your details to be kept")
valid = false;
}
if (valid==true) submit()
}

</script>

It needs to open the page volunteers_process.php

Thanks!
 
What about replacing that final line with something like

if (!valid)
{
return; //( or return false; )
}

That way it'll stop the form getting posted if something is invalid. If everything's valid, it'll get posted like normal.
 
what huppy means is this......

use this in the html for your form....

Code:
<form action="volunteers_process.php" method="post" onsubmit="return validateForm()">
.....
</form>

then you use this code at the end of your javascript to return either true or false.

Code:
if(valid == true) {
    return true;
} else {
    return false;
}

if true, the form submits to the php processing file, if false it doesn't.....

:)
 
Hi,

That doesn't seem to be doing anything.

This is what I've got so far:

Javascript function as above

<form name="volunteers" action="volunteer_process.php" align="center" method="post" onsubmit="return validateForm()">

big long form

<input type="button" class="button" value="Send" name="B1" onClick="javascript:validateForm()">
</form>

I'm pretty sure there's something simple I'm missing here
 
ah, the input type needs to be "submit"....
eg

Code:
<input type="submit" class="button" value="Send" name="B1">

also we don't need an "onclick" property, this is taken care of by the form "onsubmit". :)
 
That's got it!

Thanks a lot, it's been bugging me most of the day (well, except for the 1/2 hour when my new Benq fp241WZ arrived...)
 
Back
Top Bottom