I am updating a custom web to lead form but adding a validation error message. The error message will display and have the user to either enter an email or phone number. I was able to add this but the prior error and validation does not seem to be working, specifically the one to check if the email is formatted correctly. I can however enter the email or phone on the form, and continue to submit the form, but the lead is not coming into Salesforce. I am guessing this is because i messed up the validation somewhere. I am posting to code and any help is greatly appreciated, I have been working on this issue for far to long.
function ValidateForm() { var email = document.getElementById("email").value; if(document.getElementById("first_name").value.length == 0) { helperMsg += "Please enter First Name \n\r"; } if(document.getElementById("last_name").value.length == 0) { helperMsg += "Please enter Last Name \n\r"; } if((document.getElementById("zip").value.length != 5) && (document.getElementById("country").value == "United States")) { helperMsg += "Please enter 5 digit Zip/Postal Code number \n\r"; } if(document.getElementById("dateInput").value.length == 0) { helperMsg += "Please enter your Date of Birth \n\r"; } if(document.getElementById("dateInput").value.length != 0) { checkAge(); } if(document.getElementById("00Nt0000000H0l1").value.length != 0) { checkHt(); } if(document.getElementById("servedInMilitary").value.length === 0) { helperMsg += "Please indicate if you have Prior Military Service \n\r"; } if(getPriors === "y" && (document.getElementById("branchOfService").value.length === 0 || document.getElementById("00Nt0000000H0ld").value.length === 0)) { helperMsg += "Prior Service applicants please provide Branch of Service and Prior Rate info \n\r"; } if(grecaptcha.getResponse().length == 0) { helperMsg += "Please verify that you are not a robot \n\r"; } if((document.getElementById("email").value.length ==0 ) && (document.getElementById("00Nt0000000HwFm").value.length != 10)) { helperMsg += "Please enter a valid phone number or email \n \r"; } else { if(email.length > 80) { //check of the length of the email is more than 80 characters helperMsg += "\n- Email cannot be more than 80 characters"; } } if(helperMsg.length > 0) { alert(helperMsg); helperMsg = ""; return false; } return true; } //validates a passed text string for email format function checkEmail(str) { var at = "@"; var dot = "."; var lat = str.indexOf(at); var lstr = str.length; var ldot = str.indexOf(dot); if(str.indexOf(at) == -1) { return false; } if(str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) { return false; } if(str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) { return false; } if(str.indexOf(at, lat + 1) != -1) { return false; } if(str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) { return false; } if(str.indexOf(dot, lat + 2) == -1) { return false; } if(str.indexOf(" ") != -1) { return false; } return false; } function checkAge() { var dateString = document.getElementById("dateInput").value; //alert(dateString ); if(dateString != "") { var today = new Date(); var birthDate = new Date(dateString); var age = today.getFullYear() - birthDate.getFullYear(); var m = today.getMonth() - birthDate.getMonth(); var da = today.getDate() - birthDate.getDate(); if(m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } if(m < 0) { m += 12; } if(da < 0) { da += 30; } if(age < 15) { helperMsg += "Applicant cannot be younger than 15 years old \n\r"; } // Removed by SH for US W-001991 // else if (age > 39) { helperMsg += "Applicant cannot be older than 39 years old \n\r"; } dateString = ""; } } function checkHt() { console.log("checking height"); var ht = document.getElementById("00Nt0000000H0l1").value; if(ht < 56 || ht > 90) { helperMsg += "Please enter your height between 56 and 90 inches \n\r"; } } </script> <!-- END VALIDATION RULES -->
Hello,
Have you tried this without the validation?
It would be good to rule out if this works without your custom validation to identify that as the issue.