0

I need a text field that prevents the user from typing numbers where the user writes his/her name, like it does by default in the number fields in which you can't type letters at all. The site and form I'm working at are here: https://www.recamier.com/muss/club-muss-kids/.

And I'd like to do the same thing with the phone field, in this form I had to use a text field instead to use the minlenght:11 maxlength:11 restriction.

Luke
  • 1,736
  • 2
  • 16
  • 34

1 Answers1

0

I found a solution, you have to paste this code in the functions.php file of your active theme.

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 ( 'nombre-padre' == $tag->name ) {
$name_of_the_input = isset( $_POST['nombre-padre'] ) ? trim( $_POST['nombre-padre'] ) : '';

if ( !preg_match('/^[a-zA-ZÀ-ÿ ]+$/',$name_of_the_input) ) {
$result->invalidate( $tag, "No se permiten números en este campo" );
}
}

return $result;
}

I found this in this page https://wpquestions.com/Alphanumeric_Validation_Contact_Form_7/19587