skip to Main Content

I have a validation added in my functions.php for two fields. These validate that the field can only take letters/numbers and no special characters. It works fine but unfortunately the field ‘tussenvoegsel’ can not be left empty, which needs to be possible too.

// VALIDATIE INPUT FIELDS (GEEN CIJFERS MOGELIJK ALS INPUT)
add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );

function alphanumeric_validation_filter( $result, $tag ) {
    $tag = new WPCF7_Shortcode( $tag );
    
    if ( 'familienaam' == $tag->name ) {
        $name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
        
        if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
            $result->invalidate( $tag, "Alleen letters zijn toegestaan." );
        }
    }
    
    if ( 'tussenvoegsel' == $tag->name ) {
        $name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
        
        if ( !preg_match('/^[a-zA-Z0-9]+$/',$name_of_the_input) ) {
            $result->invalidate( $tag, "Alleen letters zijn toegestaan." );
        }
    }
    
    return $result;
} 

Which is strange as the entry in the plugin of ‘tussenvoegsel’ doesn’t have an *.
What can I add to keep the ‘tussenvoegel’ entry still optional even though I needs to validate too.

[text tussenvoegsel placeholder "voorvoegsel"]
[text* familienaam placeholder "Achternaam"]

2

Answers


  1. Using the official docs for custom validation here https://contactform7.com/2015/03/28/custom-validation/

    add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
    add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
        
    function alphanumeric_validation_filter( $result, $tag ) {
        
            if ( 'familienaam' == $tag->name ) {
             $familienaam= isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
            
                if ( !ctype_alnum( $familienaam) ) {
                   $result->invalidate( $tag, "Alleen letters zijn toegestaan." );
                }
            }
            
            if ( 'tussenvoegsel' == $tag->name ) {
                $tussenvoegsel = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
            
                 if ( !ctype_alnum( $tussenvoegsel ) ) {
                    $result->invalidate( $tag, "Alleen letters zijn toegestaan." );
                 }
             }
        
        return $result;
    }
    
    Login or Signup to reply.
  2. In your function, you’re validating the CF7 Fields, but you’re not checking to see if the field is required or not. With that being the case, it will always run your funciton through a validation filter, and require that the input is alphanumeric whether the field is required or not.

    add_filter( 'wpcf7_validate_text', 'alphanumeric_validation_filter', 20, 2 );
    add_filter( 'wpcf7_validate_text*', 'alphanumeric_validation_filter', 20, 2 );
    
    function alphanumeric_validation_filter( $result, $tag ) {
        $tag = new WPCF7_Shortcode( $tag );
    
        if ( 'familienaam' === $tag->name ) {
            $name_of_the_input = isset( $_POST['familienaam'] ) ? trim( $_POST['familienaam'] ) : '';
            // Check if the field is empty or required.
            if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
                if ( !preg_match('/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
                    $result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
                }
            }
        }
        
        if ( 'tussenvoegsel' === $tag->name ) {
            $name_of_the_input = isset( $_POST['tussenvoegsel'] ) ? trim( $_POST['tussenvoegsel'] ) : '';
            // Check if the field is empty or required.
            if ( ! empty( $name_of_the_input ) || $tag->is_required() ) {
                if ( ! preg_match( '/^[a-zA-Z0-9]+$/', $name_of_the_input ) ) {
                    $result->invalidate( $tag, 'Alleen letters zijn toegestaan.' );
                }
            }
        }
        
        return $result;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search