Marketo

Custom Validation Marketo Date Field

Recently, I came around to a problem where I need to customize the minimum date and maximum date on the Marketo form.

By default, Marketo doesn’t allow us to define the date validations on the Date field. I came around a JS to restricting the date. You can use the Javascript code below to add the validation.

<script>
MktoForms2.whenReady(function(form) {
  	var formElement = form.getFormElementem()[0],
  	oneDayFromNow = new Date(); 
            
  	oneDayFromNow.setDate(new Date().getDate() + 1);
  	var oneDayFromNowAsFullYear = [
                						oneDayFromNow.getFullYear(), 
                						("0"+(oneDayFromNow.getMonth()+1)).slice(-2),
                						("0"+(oneDayFromNow.getDate())).slice(-2)
      								].join("-");
    formElement.querySelector('#newdate').setAttribute('min', oneDayFromNowAsFullYear);

    ninemonthsFromNow = new Date(); 
            
  	ninemonthsFromNow.setMonth(new Date().getMonth() + 9);
  	var ninemonthsfromNowAsFullYear = [
                						ninemonthsFromNow.getFullYear(), 
                						("0"+(ninemonthsFromNow.getMonth()+1)).slice(-2),
                						("0"+(ninemonthsFromNow.getDate())).slice(-2)
      								].join("-");

    formElement.querySelector('#newdate').setAttribute('max', ninemonthsfromNowAsFullYear);
});       
</script>

Ref: https://nation.marketo.com/t5/Product-Discussions/Restricting-Date-Field-Value-Ranges-on-Forms/m-p/178095#M127388

Author

Anil Dhull