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 -
1 | [textarea* yoururls] |
- If we will put the url comma seperated .
- We can use below filter to custom validation in functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 ; } |
No comments:
Post a Comment