[Forminator] Set validation rule in email field to prevent emails with specific tld

How can I have emails with specific tld not be submit-able in formanator? Like I want to block an email like this: something2019@mail.test

So any emails that are ending with .test are not submit-able.

  • Mukul Chawla
    • Product Designer

    Hey Jonathan

    We actually have a task to add custom validation rules for email field. I've added your request to that task and actually raised the priority of that task. We'll add this feature to Forminator in future.

    I've also pinged our devs and SLS team to provide a custom solution for the time being.

    Mukul Chawla

  • Sohag Ronjon Dey
    • Support

    Hi Jonathan,

    Hope you are doing well :slight_smile:

    Here is a work around from our developers.

    add_filter( 'forminator_custom_form_submit_errors', function( $submit_errors, $form_id, $field_data_array ){
        $email_field = 'email-1';
        $forbitten_mail_ending = '.dev';
        $email = isset( $_POST[ $email_field ] ) ? $_POST[ $email_field ] : false;
        if ( ! $email ) {
            return $submit_errors;
        $strlen = strlen( $email );
        $testlen = strlen( $forbitten_mail_ending );
        if ( $testlen > $strlen ) return $submit_errors;
        if ( 0 === substr_compare( $email, $forbitten_mail_ending, $strlen - $testlen, $testlen ) ) {
            $submit_errors[][ $email_field ] = __( 'Invalid email address' );
        return $submit_errors;
    }, 20, 3 );

    The above code will show validation error when an email address which ends with .dev is added and the form submitted. Please replace '.dev' to the tld that you like to disallow.

    You can add the above code to your active theme's functions.php file or use it in an mu-plugin

    Hope this helps.

    Please feel free to contact us if you need anymore help.


Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.