Ask your WordPress questions! Pay money and get answers fast! (more info)

Cross Field Validation in Contact Form 7 WordPress


I need to compare and validate two text fields to make sure at least one of them says "Washington, DC".

[text* pick-up-location] [text* drop-off-location]

If one of them doesn't I need to highlight both fields and return an error message saying, 'One of these fields must be: "Washington, DC"'.

I got some hints from the code by @KZeni in [[LINK href=""]] this thread[[/LINK]] which checks the field to see if the email address is from a certain domain.

I can see from this code how to check one text field, then return the error message right inside if statement:

if($name == 'company-email'){
$the_value = $_POST[$name];
$result['valid'] = false;
$result['reason'][$name] = 'You need to provide an email address that isn\'t hosted by a free provider.<br />Please contact us directly if this isn\'t possible.';

but I don't understand how to:

* get the value from two separate elements
* compare them with the target string
* return error messages on each input field

Let me know if the question is not clear enough, thanks very much!

Answers (2)


Dbranes answers:

Hi, you can try this:

add_filter( 'wpcf7_validate', 'wpq_validate' );

function wpq_validate( $result ) {

$pick_up_location = filter_input( INPUT_POST, 'pick-up-location', FILTER_SANITIZE_STRING );
$drop_off_location = filter_input( INPUT_POST, 'drop-off-location', FILTER_SANITIZE_STRING );

$string = "washington, dc";
$error_msg = 'One of these fields must be: "Washington, DC"';

if( $string !== mb_strtolower( $pick_up_location ) && $string !== mb_strtolower( $pick_up_location ) ) {
$result['valid'] = false;
$result['reason']['pick-up-location'] = $error_msg;
$result['reason']['drop-off-location'] = $error_msg;
return $result;


You might have to adjust this to your needs.

Mike Sewell comments:

Thanks Dbranes, this looks close, but I'm just getting a white screen when I add this code although I don't see where the problem is.

Dbranes comments:

ok, your PHP version is most likely older than 5.3.

I updated the code, for older PHP versions.

Please check now.

Dbranes comments:

ps: updated again, this version should work for your PHP version ;-)

I just tested this, it seems to work on my install.

Mike Sewell comments:

Yes, that did it!

Thank you that's awesome, I'll accept your answer.

Mike Sewell comments:

If I want to add a few variations to this string how would I go about doing so?

I need to check against these stings as well: "washington dc", "dc", "washington", "washingtondc".


Arnav Joy answers:

you can use js code also ,

Use this code in foote.php before <?php wp_footer();?>

var field1 = $('#id_of_the_first_input');
var field2 = $('#id_of_the_second_input');
var val1 = val1.val();
var val2 = val2.val();

if( val1 != '' && val2 != '' ){
if( val1 != 'Washington, DC' || val2 != 'Washington, DC' ){
$(field1).css('border','1px solid red');
$(field2).css('border','1px solid red');
return false;

Please change this "id_of_the_first_input" and "id_of_the_second_input'"

Mike Sewell comments:

Thanks for the reply Arnav, but I actually need this in PHP, I should have specified in the question.