function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SunadminSunadmin 

issues with javascript form validation on web to lead page

i have web to lead page that includes some javascript code used to validate email addresses and verify all fields are filled out. the page routes to a record type but when the javascript validation is included it blocks the routing. 

here is the HTML that includes the javascript that references the validation code:

<form name="information" action= "https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" onsubmit="return validateForm();” >
 

when i take out 'onsubmit="return validateForm();” ' the information is routed to the correct record type but it wont validate the email or that the fields are filled out. the javascript code is accessed by this command.

 

here is the javascript code:

<script>
function validateForm()
{
var x=document.forms["information"]["first_name"].value;
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
var x=document.forms["information"]["last_name"].value;
if (x==null || x=="")
{
alert("Last name must be filled out");
return false;
}
var x=document.forms["information"]["email"].value;
if (x==null || x=="")
{
alert("Email must be filled out");
return false;
}
var x=document.forms["information"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
{
alert("Not a valid e-mail address");
return false;
}
var x=document.forms["information"]["confirm_email"].value;
if (x==null || x=="")
{
alert("Confirm email must be filled out");
return false;
}
var x=document.forms["information"]["email"].value;
var pass= document.getElementById("email").value
var confPass = document.getElementById("confirm_email").value
if(pass != confPass) {
alert('Email fields do not match');
return false;
}
var x=document.forms["information"]["company"].value;
if (x==null || x=="")
{
alert("Company must be filled out");
return false;
}
var x=document.forms["information"]["state"].value;
if (x==null || x=="")
{
alert("State/Province must be filled out");
return false;
}
}

</script>

 

 

PepiñoPepiño

Change the next lines:

var pass= document.getElementById("email").value
var confPass = document.getElementById("confirm_email").value

 For this:

 

var pass= document.forms["information"]["email"].value;
var confPass = document.forms["information"]["confirm_email"].value;

 And should solve your problem.

 

PD: Better late than never :P

SunadminSunadmin

Thanks Pepiño i will give this a shot.