The issue seems to be that only some of the validation is running. Currently, I have the following code.
function timestamp() { var response = document.getElementById("g-recaptcha-response"); if (response == null || response.value.trim() == "") {var elems = JSON.parse(document.getElementsByName("captcha_settings")[0].value);elems["ts"] = JSON.stringify(new Date().getTime());document.getElementsByName("captcha_settings")[0].value = JSON.stringify(elems); } }
setInterval(timestamp, 500);
function validateForm() {
var name = document.WebToCaseForm.name.value;
var email = document.WebToCaseForm.email.value;
var phone = document.WebToCaseForm.phone.value;
var reason = document.WebToCaseForm.type.value;
var regName = /^[a-zA-Z]+ [a-zA-Z]+$/;
var regEmail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
var regPhone = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
//VALIDATION FOR MANDATORY FIELDS
/*
if (name == "") {
alert("Name must be filled out");
return false;
}
if (email == "") {
alert("Email must be filled out");
return false;
}
if (phone == "") {
alert("Phone must be filled out");
return false;
}
if (reason == "") {
alert("Please provide a Reason For Contacting Us");
return false;
}
if (document.getElementById("captchaChecked").value != "true") {
alert("Please click 'I'm not a robot'");
return false;
}
*/
//VALIDATION FOR FOMATTING
if(!regName.test(name)){
alert('Invalid name given. Please provide First Name and Last Name');
return false;
}
if (!regEmail.test(email)){
alert('Invalid email given. Please provide a valid email address');
return false;
}
if (!regPhone.test(phone)) {
alert('Invalid phone number given. Please provide a valid phone number ie 123-456-7890');
return false;
}
alert('Form submitted successfully. Please check your email.');
}
function recaptchaCallback() {
document.getElementById("captchaChecked").value = "true";
}
You can see I've commented out the VALIDATION FOR MANDATORY FIELDS section:
//VALIDATION FOR MANDATORY FIELDS
/*
if (name == "") {
alert("Name must be filled out");
return false;
}
if (email == "") {
alert("Email must be filled out");
return false;
}
if (phone == "") {
alert("Phone must be filled out");
return false;
}
if (reason == "") {
alert("Please provide a Reason For Contacting Us");
return false;
}
if (document.getElementById("captchaChecked").value != "true") {
alert("Please click 'I'm not a robot'");
return false;
}
*/
I've done this because (1) the name, email, and phone validation seemed redundant based on the subsequent formatting validation, and (2) it's the only way to get the formatting validation to run correctly.
If I un-comment the "reason" and "captcha" validation, the formatting validation doesn't run.
Am I missing something? I'm also working with my internal developer, but he's not a HubSpot user, so I feel like something's lost in translation when I brought this JS over to HubSpot.
As I'm reverse-engineering this form, it seems there was quite a bit of customization "after the fact." The reason for the code not working correctly was that I was missing a couple of lines of code in the form itself. Specifically, I was missing:
"hidden" input value for the "recaptcha"
and the data-callback value for the recaptcha
Both of these were referred to in the JS, but weren't present in the form code, so the JS skipped validating those parts.
I really don't like reverse-engineering old code...lol
As I'm reverse-engineering this form, it seems there was quite a bit of customization "after the fact." The reason for the code not working correctly was that I was missing a couple of lines of code in the form itself. Specifically, I was missing:
"hidden" input value for the "recaptcha"
and the data-callback value for the recaptcha
Both of these were referred to in the JS, but weren't present in the form code, so the JS skipped validating those parts.
I really don't like reverse-engineering old code...lol
Join us on March 27th at 12 PM for the Digital Essentials Lab, an interactive session designed to redefine your digital strategy!
Engage with expert Jourdan Guyton to gain actionable insights, participate in live Q&A, and learn strategies to boost your business success. Don't miss this opportunity to connect and grow—reserve your spot today!