SOLVED

Email validation in Marketo through Javascript

Go to solution
Phillip_Wild
Level 10

Hi Community!

We've been finding that unfortunately Marketo forms have pretty crappy email validation on them as default. Using the "Email" field means that a user is only required to enter an email with an "@" symbol - they don't need a period "." This means we get a bunch of junk emails that fail validation in our other systems.

Here's the code my developer gave me to solve this:

<script>

  MktoForms2.loadForm("//app-ab06.marketo.com", "110-AIL-152", 1330, function(form){

      form.onValidate(function(){

        // Get the values

        var vals = form.vals();

        var re = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);

        if(re.test(vals.Email)){

          form.submittable(true);

        }

        else{

          form.submittable(false);

          var emailElem = form.getFormElem().find("#Email");

          form.showErrorMessage("Please enter a valid email format", emailElem);

        }

      });

    })

</script>

If I was using a "self-guided" landing page, I know how to implement this - simply create a HTML element and drop this script in the HTML code section. But with a guided landing page, it doesn't seem to work.

I've tried two ways - dumping it at the bottom of the raw code (after "editing draft" on my template), below where the other scripts are, and secondly, dropping it into a section via the landing page editor (much like I would with a "self-guided" page). Neither seems to work.

Any ideas? Is there an easier way to do this? I know you can "mask input" with fields in Marketo forms - is this a better way of doing it? Will there be disadvantages if I use this field type instead of "Email" field type?

Thanks in advance!

Phil

Tags (2)
1 ACCEPTED SOLUTION
Grégoire_Miche2
Level 10

Hi Phillip,

Try this at the bottom of the guided LP template:

<script>

MktoForms2.whenReady(function (form) {

      form.onValidate(function(){

        // Get the values

        var vals = form.vals();

        var re = new RegExp(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i);

        if(re.test(vals.Email)){

          form.submittable(true);

        }

        else{

          form.submittable(false);

          var emailElem = form.getFormElem().find("#Email");

          form.showErrorMessage("Please enter a valid email format", emailElem);

        }

      });

});

</script>

-Greg

View solution in original post

15 REPLIES 15