Expert Group of programming
Loading...

Server side validation in Contact form 7 plugin

Posted by : Heycode Tech | Posted in : wordpress, WordPress Functions, WordPress Plugins | Jul 31,2019 | 0 comments

In contact form 7 plugin, we are facing one challenge for server side validation. Now we have received a HOOK from contact form 7 official website for implementing the server side validation.

I have used in own code and after using I become very glad because we achieve the custom server side validation in Plugin – Contact form 7.

Below we have presented the code which is used for implementing the server side validation. You need to put this code in file : functions.php :

For using this function, you have to keep in mind it’s shortcode name. But still any problem you can email me through contact page.

Let’s see it.

Here are very easy way for implementing the server side validation in PHP.

For the textfield :

We have added the validation of following conditions –
1 : If name is empty.
2 : If name has special characters.

Name:         [text* your-name]

For implementing the server side validation,we need to put the content in file : functions.php : Below is the code

add_filter( 'wpcf7_validate_text*', 'custom_text_validation_filter', 20, 2 );
function custom_text_validation_filter( $result, $tag ) {

        $person_name = isset($_POST['your-name']) ? trim($_POST['your-name']) : '';
        $valid_text = "/^[A-Za-z\s]+$/";  
      if(empty($person_name))  {                
      $validation_message ="Please Enter  Name !";
	  $result->invalidate( $tag, $validation_message);
    }
    else{
    if(!preg_match($valid_text, $person_name)) {   
      $result->invalidate( $tag, $validation_message);    
       }         
    } 
    return $result;
}

Email id validation

Email:         [email* your-email]
add_filter( 'wpcf7_validate_email*', 'custom_email_confirmation_validation_filter', 20, 2 );
function custom_email_confirmation_validation_filter( $result, $tag ) {
 $your_email = isset( $_POST['your-email'] ) ? trim( $_POST['your-email'] ) : '';
  	  if(empty($email))  {                
      $validation_message ="Please Enter Email !";  
      $result->invalidate( $tag, $validation_message );         
    }     
    else{
    if(!preg_match($valid_email, $email)){
      $validation_message= 'Please Enter Valid Email !';      
     $result->invalidate( $tag, $validation_message );    
    }
    }
    return $result;
}

Textarea validation

CF7 CODE : Comment: [textarea* comment]

Actually we are suffering from : SPAM CONTENT : in contact form 7 so it is very important validation to prevent these types of content through validation.
Below is the validation code which you need to put in file : functions.php : .
Suppose if you want to prevent these strings
Strings : sexy, porn

add_filter( 'wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 20, 2 );
 
function custom_textarea_validation_filter( $result, $tag ) {
      $comment = isset($_POST['comment']) ? trim($_POST['comment']) : '';
        $stringPreventArr =array('sexy','porn');
        $stringPrevent = implode(',',$stringPreventArr);
        //validate message
        if($stringPreventArr){
          foreach ($stringPreventArr as $strObj) {
                if (strpos($comment, $strObj) !== false) {
                    $result->invalidate( $tag, "Some strings are malicious.Please don't use these : $stringPrevent ." );               break;
                }
          }
        }
    return $result;
}

See below image as a outout.

For more info,you can visit on here : https://contactform7.com/2015/03/28/custom-validation/

Tags:

Author information - Heycode Tech

Every world can ask question and my website registered user give the suggestion.

See all posts by Author

Connect to author

Leave a Reply

Your email address will not be published. Required fields are marked *

About us

Heycodetech is a platform where you can ask any questions related to the programming like PHP,jQuery, MYSQL,HTML , CSS and more Here is the Expert group of programming.