<?php
add_filter( 'wpcf7_validate_tel*', 'ds1_custom_tel_confirmation_validation_filter', 20, 2 );

function ds1_custom_tel_confirmation_validation_filter( $result, $tag ) {
	//$result->invalidate( $tag, "Are you sure this is the correct address 2?");
	if ( 'your-phone' == $tag->name &&  isset( $_POST['your-phone']) &&  isset( $_POST['country_list']) ) {
		$your_phone 	= htmlspecialchars( $_POST['your-phone'] );
		$country_list 	= (int) $_POST['country_list'];

		$code_c_vald = array(
			"p-20" => array('min'=>11, 'max'=>11),
			"p-966" => array('min'=>10, 'max'=>15),
			"p-971" => array('min'=>10, 'max'=>15),
			"p-961" => array('min'=>10, 'max'=>15),
			"p-962" => array('min'=>10, 'max'=>15),
			"p-963" => array('min'=>10, 'max'=>15),
			"p-964" => array('min'=>10, 'max'=>15),
		);


		$check_ii = ($country_list && isset($code_c_vald['p-'.$country_list])) ? true : false;
	
		$min = ($check_ii) ? $code_c_vald['p-'.$country_list]['min'] : 10;
		$max = ($check_ii) ? $code_c_vald['p-'.$country_list]['max'] : 13;

		if (
			strlen($your_phone) < $min || 
			strlen($your_phone) > $max
		) {
			// error message
			$result->invalidate( $tag, "Please enter a valid phone number" );
		}
		if ( $country_list == 20 && ! in_array(substr($your_phone, 0,3), array('010','011','012','015') )) {
			// error message
			$result->invalidate( $tag, "Enter a valid mobile number" );
		}

  }
  
  return $result;
}
