Note: this tutorial is meant for the premium version.

You may want to add some custom validation on one of the fields displayed on the opt-in form. You can do so by adding some custom Javascript code to your theme. Not sure how to add Javascript to your theme? It’s really easy! Read this article for more information.

Example 1: only contain numbers

Below is the code to only allow numbers. This can be handy to validate a phone number in a text field.

WOF.Dispatcher.addFilter('wof-validate-MMERGE3',function(data){
    var value = data.value;
    if(!value) return false;
    return /\d/.test(value);
});

Take note of the filter id ‘wof-validate-MMERGE3’. Replace MMERGE3 with the ID of your field. The ID can be found in your email list software such as MailChimp.

Example 2: enter a date before January 1, 2018

This advanced example will test that a date entered is before 1 January 2018:

WOF.Dispatcher.addFilter('wof-validate-MMERGE3',function(data) {
    var value = data.value.split('/');
    if(!value) return false;
    if(value.length != 3) return false;
    try{
        var d = new Date(value[2]+'-'+value[1]+'-'+value[0]+'Z');
        var dec1 = new Date('2018-01-01Z');
        return d >= dec1;
    }
    catch(err) {
        return false;
    }
});