carolina
09-25-2006, 12:26 AM
Hi,
I am trying to validate a form with text boxes, drop downs, radio buttons and check boxes using javascript.
The form is a form to email script using php.
I have managed to figure out the start of the javascript validating but it doesn't work. The confirmation of email1 and email2 is the only thing that works at the moment.
Below is the script - I have text box and drop down validation and also a function validating email1 and email2 (confirming that they are both identical)
If anyone can understand what's going wrong, any help would be much appreciated.
Javascript:
<!-- validate Form (open function)//-->
function validate()
{
for(i=0; i<document.form1.elements.length;i++)
{
document.form1.elements[i].style.backgroundColor="#FFFFFF";
}
<!-- validate firtname (Text Box) required //-->
if(document.form1.strName.value.length==0)
{
alert("Enter a Name");
document.form1.firstname.focus();
document.form1.firstname.select();
return false;
}
<!-- validate lastname (Text Box) required //-->
if(document.form1.strName.value.length==0)
{
alert("Enter a Name");
document.form1.lastname.focus();
document.form1.lastname.select();
return false;
}
<!-- validate Postcode (Text Box) number required//-->
if(document.form1.postcode.value.length==0)
{
alert("What is your contact number?");
document.form1.postcode.focus();
document.form1.postcode.select();
return false;
}
if(isNaN(document.form1.postcode.value))
{
alert("Number must be a number");
document.form1.postcode.focus();
document.form1.postcode.select();
return false;
}
<!-- validate Email (Text Box) characters: @ . required //-->
var intAtSymbol = document.form1.email.value.indexOf("@");
var intDotSymbol = document.form1.email.value.indexOf(".");
var intLength = document.form1.email.value.length;
if(intAtSymbol == 0 || intAtSymbol > intLength-5)
{
alert("Email must contain valid @ eg: name@email.com")
document.form1.email.focus();
document.form1.email.select();
return false;
}
if(intDotSymbol < (intAtSymbol+2)|| intDotSymbol > (intLength-3))
{
alert("Email must contain valid . eg: name@email.com")
document.form1.email.focus();
document.form1.email.select();
return false;
}
<!-- validate Phone (Text Box) required number//-->
if(document.form1.phone.value.length==0)
{
alert("What is your phone number?");
document.form1.phone.focus();
document.form1.phone.select();
return false;
}
if(isNaN(document.form1.phone.value))
{
alert("Phone Number must be a number");
document.form1.phone.focus();
document.form1.phone.select();
return false;
}
<!-- validate title (Drop Down) required //-->
if ( document.form1.title.selectedIndex == 0 )
{
alert ( "Please Select a Title." );
return false;
}
<!-- validate age (Drop Down) required //-->
if ( document.form1.age.selectedIndex == 0 )
{
alert ( "Please Select an Age." );
return false;
}
<!-- validate occupation (Drop Down) required //-->
if ( document.form1.occupation.selectedIndex == 0 )
{
alert ( "Please Select an Occupation." );
return false;
}
<!-- validate children (Drop Down) required //-->
if ( document.form1.children.selectedIndex == 0 )
{
alert ( "Please Select number of Children." );
return false;
}
<!-- validate relationship (Drop Down) required //-->
if ( document.form1.relationship.selectedIndex == 0 )
{
alert ( "Please Select Type of Relationship." );
return false;
}
}
function check(a,b) {
var obja = document.getElementById(a)
var objb = document.getElementById(b)
if (obja.value==objb.value) {
} else {
alert("Email address does not match, please re-type")
}
}
//-->
First line from Form Code:
<form action="joinonline_test02.php" method="post" name="form1" onSubmit="check('email', 'email2'); return false">
I am trying to validate a form with text boxes, drop downs, radio buttons and check boxes using javascript.
The form is a form to email script using php.
I have managed to figure out the start of the javascript validating but it doesn't work. The confirmation of email1 and email2 is the only thing that works at the moment.
Below is the script - I have text box and drop down validation and also a function validating email1 and email2 (confirming that they are both identical)
If anyone can understand what's going wrong, any help would be much appreciated.
Javascript:
<!-- validate Form (open function)//-->
function validate()
{
for(i=0; i<document.form1.elements.length;i++)
{
document.form1.elements[i].style.backgroundColor="#FFFFFF";
}
<!-- validate firtname (Text Box) required //-->
if(document.form1.strName.value.length==0)
{
alert("Enter a Name");
document.form1.firstname.focus();
document.form1.firstname.select();
return false;
}
<!-- validate lastname (Text Box) required //-->
if(document.form1.strName.value.length==0)
{
alert("Enter a Name");
document.form1.lastname.focus();
document.form1.lastname.select();
return false;
}
<!-- validate Postcode (Text Box) number required//-->
if(document.form1.postcode.value.length==0)
{
alert("What is your contact number?");
document.form1.postcode.focus();
document.form1.postcode.select();
return false;
}
if(isNaN(document.form1.postcode.value))
{
alert("Number must be a number");
document.form1.postcode.focus();
document.form1.postcode.select();
return false;
}
<!-- validate Email (Text Box) characters: @ . required //-->
var intAtSymbol = document.form1.email.value.indexOf("@");
var intDotSymbol = document.form1.email.value.indexOf(".");
var intLength = document.form1.email.value.length;
if(intAtSymbol == 0 || intAtSymbol > intLength-5)
{
alert("Email must contain valid @ eg: name@email.com")
document.form1.email.focus();
document.form1.email.select();
return false;
}
if(intDotSymbol < (intAtSymbol+2)|| intDotSymbol > (intLength-3))
{
alert("Email must contain valid . eg: name@email.com")
document.form1.email.focus();
document.form1.email.select();
return false;
}
<!-- validate Phone (Text Box) required number//-->
if(document.form1.phone.value.length==0)
{
alert("What is your phone number?");
document.form1.phone.focus();
document.form1.phone.select();
return false;
}
if(isNaN(document.form1.phone.value))
{
alert("Phone Number must be a number");
document.form1.phone.focus();
document.form1.phone.select();
return false;
}
<!-- validate title (Drop Down) required //-->
if ( document.form1.title.selectedIndex == 0 )
{
alert ( "Please Select a Title." );
return false;
}
<!-- validate age (Drop Down) required //-->
if ( document.form1.age.selectedIndex == 0 )
{
alert ( "Please Select an Age." );
return false;
}
<!-- validate occupation (Drop Down) required //-->
if ( document.form1.occupation.selectedIndex == 0 )
{
alert ( "Please Select an Occupation." );
return false;
}
<!-- validate children (Drop Down) required //-->
if ( document.form1.children.selectedIndex == 0 )
{
alert ( "Please Select number of Children." );
return false;
}
<!-- validate relationship (Drop Down) required //-->
if ( document.form1.relationship.selectedIndex == 0 )
{
alert ( "Please Select Type of Relationship." );
return false;
}
}
function check(a,b) {
var obja = document.getElementById(a)
var objb = document.getElementById(b)
if (obja.value==objb.value) {
} else {
alert("Email address does not match, please re-type")
}
}
//-->
First line from Form Code:
<form action="joinonline_test02.php" method="post" name="form1" onSubmit="check('email', 'email2'); return false">