If you’re at least slightly familiar with WordPress, then you’ve come across a need for a admin managed form generator. In more specific terms, you’ve come across the most popular plugin to achieve this in Contact Form 7(CF7). CF7 is a lot of great things and its documentation does a decent job of providing assistance. Well, except for name validation.
Now I’ve used CF7 quite a bit with the majority of client work and theme development, but the common issue I find is there being no built in name validation. So with the help of this comment from the plugin author, I was able to make a quick validation function for any input field with a name value containing the word name. See below.
function valid_name( $result, $tag ) { $type = $tag['type']; $name = $tag['name']; if (strpos($name, 'name')) { if(preg_match('#[0-9]#',$_POST[$name]) || preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/',$_POST[$name])) { // Number string must equal this $result['valid'] = false; $result['reason'][$name] = 'Please enter a valid name'; } } return $result; } add_filter( 'wpcf7_validate_text', 'valid_name', 10, 2 ); add_filter( 'wpcf7_validate_text*', 'valid_name', 10, 2 );