Question: How we can validate comma separated urls in for Contact form 7 plugin?
Answer: We can do this with contact form 7 filter.
For Example:
- If in form field is -
[textarea* yoururls]
- If we will put the url comma seperated .
- We can use below filter to custom validation in functions.php
add_filter( 'wpcf7_validate_textarea*', 'function_validate_urls', 20, 2 );
function function_validate_urls( $result, $tag ) {
if ( 'yoururls' == $tag->name ) {
$yoururls = isset( $_POST['yoururls'] ) ? trim( $_POST['yoururls'] ) : '';
$error='noerror';
$sepurl = explode(",",$yoururls);
foreach($sepurl as $sep){
$sep = preg_replace('/\s+/', '', $sep);
if($sep!=''){
if (filter_var($sep, FILTER_VALIDATE_URL)) {
// valid
} else {
//invalid
$error= 'error';
}
}
}
if ( $error == 'error' ) {
$result->invalidate( $tag, "Please check all urls in exact format." );
// $result->invalidate( $tag, $error );
}
}
return $result;
}