phpwordpressvalidationcontact-form-7

How to implement letter-only for Wordpress contact form 7 plugin


Please give me example of letter-only validation for wordpress- contact form 7 plugin.

I already read about it in the Wordpress forum, but I didn't see how to implement it.


Solution

  • This is regex form letter only validation

    /^[a-zA-Z]+$/

    Or

    Add following code in function.php. Change fields name according to your form

    function my_wpcf7_validate_text( $result, $tag ) {
    
        $type = $tag['type'];
        $name = $tag['name'];
        $value = $_POST[$name] ;
    
        if ( strpos( $name , 'name' ) !== false ){
        $regex = '/^[a-zA-Z]+$/';
        $Valid = preg_match($regex,  $value, $matches );
                if ( $Valid > 0 ) {
                }
                else {
                    $result->invalidate( $tag, wpcf7_get_message( 'invalid_name' ) );
                }
            }
            return $result;
    }
    add_filter( 'wpcf7_validate_text*', 'my_wpcf7_validate_text' , 10, 2 );
    
    add_filter( 'wpcf7_messages', 'mywpcf7_text_messages' );
    function mywpcf7_text_messages( $messages ) {
        return array_merge( $messages, array(
                                             'invalid_name' => array(
                                                                     'description' => __( "Name is invalid", 'contact-form-7' ),
                                                                     'default' => __( 'Name seems invalid.', 'contact-form-7' )
                                                                     )));
    }