How to Validate Textarea Input Field with Alphabets or Number with Space in Contact Form 7 WordPress

preview_player
Показать описание
In this wordpress contact form 7 tutorial you will learn how to add validation on textarea input field for alphabet (a-z) & numbers (0-9) and space using custom code in cf7 wordpress. So you can add custom php snippet then add your textarea field tag in code and also you can customize error message according to your requirement. So only allow alphabet character or number or both with space in form field and if user add any special characters then form submission / submit not done and show notice.

* Find Code in Top Pinned Comment Section.

* Add Alphanumeric Validation using Custom Code.

#cf7 #contactform7 #textarea #number #space #text #alphabet #field #input #validation #wordpress #wordpresstutorial #webtaskwithhassan #hassangilani
Рекомендации по теме
Комментарии
Автор

Use this code:

add_filter( 'wpcf7_validate_textarea', 'alphanumeric_validation_filter_textarea', 20, 2 );
add_filter( 'wpcf7_validate_textarea*', 'alphanumeric_validation_filter_textarea', 20, 2 );

function $result, $tag ) {
$tag = new WPCF7_Shortcode( $tag );

if ( 'your-message' == $tag->name ) {
$name_of_the_input = isset( $_POST['your-message'] ) ? trim( $_POST['your-message'] ) : '';

if ( !preg_match('/^[a-zA-Z0-9\s]+$/', $name_of_the_input) ) {
$result->invalidate( $tag, "Allowed characters are alphanumeric only" );
} elseif (!(preg_match('/[A-Za-z]/', $name_of_the_input) || preg_match('/[0-9]/', $name_of_the_input) || preg_match('/[\s]/', $name_of_the_input))) {
$result->invalidate( $tag, "Must contain Alphabets and Numbers Only!" );
}
}

return $result;
}

WebTaskWithHassan