views:

14

answers:

0

right now I use a simple formmail script with some JavaScript form validation.

I think what I need to do is add to the JavaScript an if statement which says

if all of these fields validate go to this page, if only 10 fields validate go to this page, if those 10 fields don't validate, produce the standard error messages.

the problem is I need to make sure as well as doing this it still passes through the formmail script and sends an email if it goes to one of the 2 thank you pages.

how would I modify the below JavaScript to produce this, i am not very familiar with javascript?

<script type="text/javascript">
function validateFormOnSubmit(theForm) {
var reason = "";
  reason += validateEmail(theForm.Email);
  reason += validatePhone(theForm.Telephone);
  reason += validateRequired(theForm.Name);
  reason += validateRequired(theForm.Surname);
  reason += validateRequired(theForm.Postcode);
  reason += validateRequired(theForm.hos);
  reason += validateRequired(theForm.Ptype);
  reason += validateRequired(theForm.Purpose);
  reason += validateRequired(theForm.Lamount);
  reason += validateRequired(theForm.Address1);
  if (reason != "") {
    alert("Please Complete All The Required Fields - We need all of this information in order to consider your application.\n" + reason);
    return false;
  }

  return true;
}
function validateRequired(fld) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "Please confirm all the drop down options.\n";
    }  else if
            (fld.value == "Your Name*") {
                fld.style.background = 'Yellow'; 
                error = "Please Fill In Your Name.\n";
    }  else if
    (fld.value == "Surname*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your Surname.\n";
    }  else if
    (fld.value == "Postcode*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your Postcode.\n";
    }  else if
    (fld.value == "Home Ownership Status*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your hos.\n";
    }  else if
    (fld.value == "Pension Type*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your PType.\n";
    }  else if
    (fld.value == "Purpose Of Loan*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your Purpose.\n";
    }  else if
    (fld.value == "Loan Amount*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Your Lamount.\n";
    }  else if
    (fld.value == "Address Line 1*") {
        fld.style.background = 'Yellow'; 
        error = "Please Fill In Address Line 1.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Please enter a valid phone number.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 1) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 2) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 3) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 4) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 5) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 6) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 7) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 8) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else if (stripped.length == 9) {
        error = "Please enter a valid phone number. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
</script>