Basic form validation Email and Phone numbers code provided

Anonymous
Not applicable

Basic form validation Email and Phone numbers code provided

Hi all,

As i spend a bit of time today getting this figured out I thought i'd post the SOLUTION here on this form. The code posted below will provide basic validation rules for Email and Phone number fields before leads are submitting a form, so that leads are synced to salesforce without a problem. So for those interested please continue reading.

On your landing page that contains the form, drag a custom HTML element onto the landing page and add the following code snippet. Please not that you may need to change the field names e.g. #Email and #Phone.

I hope this is useful and save a bunch of time for someone.

Gerko

<script type="text/javascript" src="/js/public/jquery-latest.min.js" language="Javascript"></script>
<script type="text/javascript">
    // set no conflict mode for jquery
 var $jQ = jQuery.noConflict();

 function myFormIsValid() {
   var thisIsValid = true;

   var emailReg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
   //The phone number is very basic validation, for a good list of variations check out: http://regexlib.com/Search.aspx?k=phone+number&c=-1&m=-1&ps=20
   var phonenumberReg = /^(\+?\-? *[0-9]+)([,0-9 ]*)([0-9 ])*$)|(^ *$/i;
   var emailaddressVal = $("#Email").val();
   var phonenumberVal = $("#Phone").val();

  //Validate the email field
  if(!emailReg.test(emailaddressVal)) {
    Mkto.setError($jQ("#Email ~ span").prev()[0],"Please provide a valid email address");
    thisIsValid = false;

   }else {
      Mkto.clearError($jQ("#Email ~ span").prev()[0] );
   }

  //Check the phone number field
  if(!phonenumberReg.test(phonenumberVal)) {
    Mkto.setError($jQ("#Phone ~ span").prev()[0],"Please provide a valid phone number");
    thisIsValid = false;

   }else {
      Mkto.clearError($jQ("#Email ~ span").prev()[0] );
   }

   return thisIsValid;
 }

 function formSubmit(elt) {
   if (!myFormIsValid()) {
      return false;
   }
   return Mkto.formSubmit(elt);
 }
</script>
Tags (1)
2 REPLIES 2
Anonymous
Not applicable

Re: Basic form validation Email and Phone numbers code provided

Gerko -- this is very interesting. Could you explain a bit what you're considering a "valid" email or phone number? I don't speak RegEx too well 🙂
Nora_Slattery3
Level 4

Re: Basic form validation Email and Phone numbers code provided

Hi Gerko,

Thank you for this!

Is there any chance you could add some comments - just to make sure I'm understanding the code correctly?

Warm regards,
Nora Slattery