esthera
02-02-2005, 06:51 AM
I have the following validation code. For some strange reason it is letting the form submit even when the fields are empty.
It is strange as it works for a few of the fields. it works for name,zip,email (but just the validation for blank email and not valid email) The rest don't seem to work.
Can anyone spot what I am doing wrong?
function checkMyForm() {
if ( document.Form.firstname.value == "" )
{
alert ( "Please fill in the 'First Name' box." );
setFocus(document.Form.firstname);
return false;
}
else if ( document.Form.lastname.value == "" )
{
alert ( "Please fill in the 'Last Name' box." );
setFocus(document.Form.lastname);
return false;
}
else if ( document.Form.address.value == "" )
{
alert ( "Please fill in the 'Address' box." );
setFocus(document.Form.lastname);
return false;
}
else if ( document.Form.city.value == "" )
{
alert ( "Please fill in the 'City' box." );
setFocus(document.Form.city);
return false;
}
else if (document.Form.zip.value.length <= "0"){
alert("Please enter a zip.");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.zip.value.length < "5"){
alert("Please enter a zip.");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.zip.value.length > "5"){
alert("Zip code can only be 5 numbers");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.email.value == "")
{
alert ( "Please fill in the Email box." );
setFocus(document.Form.email);
return false;
}
else if (!isEmailAddr(document.form.email.value)){
alert("Please Enter a valid email address");
setFocus(document.Form.email);
return false;
}
else if (document.Form.home_phone1.value== "")
{
alert("Area Code cannot be blank");
setFocus(document.Form.home_phone1);
return false;
}
else if (document.Form.home_phone2.value== "")
{
alert("Home phone cannot be blank");
setFocus(document.Form.home_phone2);
return false;
}
else if (document.Form.home_phone3.value== "")
{
alert("Home phone cannot be blank");
setFocus(document.Form.home_phone3);
return false;
}
else if (document.Form.amount.value== "0"){
alert("Please choose a amount");
setFocus(document.Form.amount);
return false;
}
else {
return true;
}
}
function isEmailAddr(email)
{
var result = false
var theStr = new String(email)
var index = theStr.indexOf("@");
if (index > 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}
return result;
}
function setFocus(aField) {
aField.focus();
}
It is strange as it works for a few of the fields. it works for name,zip,email (but just the validation for blank email and not valid email) The rest don't seem to work.
Can anyone spot what I am doing wrong?
function checkMyForm() {
if ( document.Form.firstname.value == "" )
{
alert ( "Please fill in the 'First Name' box." );
setFocus(document.Form.firstname);
return false;
}
else if ( document.Form.lastname.value == "" )
{
alert ( "Please fill in the 'Last Name' box." );
setFocus(document.Form.lastname);
return false;
}
else if ( document.Form.address.value == "" )
{
alert ( "Please fill in the 'Address' box." );
setFocus(document.Form.lastname);
return false;
}
else if ( document.Form.city.value == "" )
{
alert ( "Please fill in the 'City' box." );
setFocus(document.Form.city);
return false;
}
else if (document.Form.zip.value.length <= "0"){
alert("Please enter a zip.");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.zip.value.length < "5"){
alert("Please enter a zip.");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.zip.value.length > "5"){
alert("Zip code can only be 5 numbers");
setFocus(document.Form.zip);
return false;
}
else if (document.Form.email.value == "")
{
alert ( "Please fill in the Email box." );
setFocus(document.Form.email);
return false;
}
else if (!isEmailAddr(document.form.email.value)){
alert("Please Enter a valid email address");
setFocus(document.Form.email);
return false;
}
else if (document.Form.home_phone1.value== "")
{
alert("Area Code cannot be blank");
setFocus(document.Form.home_phone1);
return false;
}
else if (document.Form.home_phone2.value== "")
{
alert("Home phone cannot be blank");
setFocus(document.Form.home_phone2);
return false;
}
else if (document.Form.home_phone3.value== "")
{
alert("Home phone cannot be blank");
setFocus(document.Form.home_phone3);
return false;
}
else if (document.Form.amount.value== "0"){
alert("Please choose a amount");
setFocus(document.Form.amount);
return false;
}
else {
return true;
}
}
function isEmailAddr(email)
{
var result = false
var theStr = new String(email)
var index = theStr.indexOf("@");
if (index > 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}
return result;
}
function setFocus(aField) {
aField.focus();
}