Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save developer-anuragsingh/8354dca8267aa1b9daad63e2e32df3e9 to your computer and use it in GitHub Desktop.

Select an option

Save developer-anuragsingh/8354dca8267aa1b9daad63e2e32df3e9 to your computer and use it in GitHub Desktop.
// Validate CF7 text field for name
function custom_name_field_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
//here textbox type name is 'subject'
if($name == 'first-name' || $name == 'last-name') {
$value = $_POST[$name];
if (preg_match('/[\^£$%*()}{#~><>|=_+¬]/', $value)){
$result->invalidate( $tag, "Invalid characters." );
}
}
return $result;
}
add_filter('wpcf7_validate_text','custom_name_field_validation_filter', 10, 2);
add_filter('wpcf7_validate_text*', 'custom_name_field_validation_filter', 10, 2);
// Validate CF7 text field
function custom_text_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
//here textbox type name is 'subject'
if($name == 'city' || $name == 'state' || $name == 'zip') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{@#~><>|=_+¬]/', $value)){
$result->invalidate( $tag, "Invalid characters." );
}
}
return $result;
}
add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2);
// Validate CF7 textarea
function custom_textarea_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'message' || $name == 'address') {
$value = $_POST[$name];
if (preg_match('/[\'^£$%&*()}{@#~><>|=_+¬]/', $value)){
$result->invalidate( $tag, "Invalid characters." );
}
}
return $result;
}
// add_filter('wpcf7_validate_textarea','custom_textarea_validation_filter', 10, 2);
// add_filter('wpcf7_validate_textarea*', 'custom_textarea_validation_filter', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment