4

I'm trying to add regular expression validation to Contact Form 7 'last-name' field that will allow for hyphenated names. I have researched and written a function to allow for this, but it doesn't seemed to be working. Any help would be appreciated.

Here is the function I have written and placed in functions.php file...

add_filter('wpcf7_validate_text', 'custom_text_validation', 20, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation', 20, 2);

function custom_text_validation($result, $tag) {
    $type = $tag['type'];
    $name = $tag['name'];

    if($name == 'last-name') {
        $value = $_POST[$name];
        if(!preg_match('[a-zA-Z\-]', $value)){
            $result->invalidate($tag, "Invalid characters");
        }
    }
    return $result;
}
Herowe
  • 43
  • 1
  • 4

2 Answers2

7

So the first thing I think we will need to look at it is on your 5th and 6th lines. According to the CF7 documentation, the $tag argument actually returns an object and not an array.

Which means that $tag['name'] and $tag['type'] should actually be $tag->name and $tag->type.

The second thing to address is your regex expression, now would be a good time to read up on Falsehoods Programmers Believe about Names. Basically, in short, there are a lot of last names that will not match if the criteria is MixedAlpha and a dash.

If, however, you are intent on cutting out a portion of potential users, might I suggest making use maček's basic regex listed on this SO answer as it will atleast include a few more potential valid last names.

This would turn your function into something like this:

add_filter('wpcf7_validate_text', 'custom_text_validation', 20, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation', 20, 2);

function custom_text_validation($result, $tag) {
    $type = $tag->type; //object instead of array
    $name = $tag->name; //object instead of array

    if($name == 'last-name') {
        $value = $_POST[$name];
        if(!preg_match("/^[a-z ,.'-]+$/i", $value )){ //new regex statement
            $result->invalidate($tag, "Invalid characters");
        }
    }
    return $result;
}
Frits
  • 7,341
  • 10
  • 42
  • 60
  • Thanks for helping me clean some of the code up. The form is still accepting numbers without any validation. Is there anything I could be missing? – Herowe Jul 25 '17 at 20:09
  • Anytime. Are you 100% sure that you got the input name right? It's not 'lastname' or anything like that? If it's still allowing numbers, then it's failing either in one of the two IF statements. My first guess would be that the input name is wrong? Can you double check that? – Frits Jul 25 '17 at 20:18
  • Yes I'm positive the input name is 'last-name'. Here's a part of the form...
    [text* first-name placeholder "First Name"]
    [text* last-name placeholder "Last Name"]
    – Herowe Jul 25 '17 at 20:22
  • Ok cool. Can you try to increase the priority on the two add_filter functions from 20 to 100? – Frits Jul 25 '17 at 20:27
  • Thanks for your help. I found out the employee developing the site before me had added the jquery validation plugin which was overriding. – Herowe Jul 25 '17 at 20:57
1

Try negating it

if(preg_match('/[^a-z\-]/i', $value)){

I've also updated it to use /i, which will ignore case

adprocas
  • 1,863
  • 1
  • 14
  • 31
  • Thanks, but this still doesn't work. The form still accepts any characters. It also seems you added numeric values. The input field can only accept letters and hyphens. – Herowe Jul 25 '17 at 18:45
  • It looks like, based on the accepted answer, you had other issues that were likely causing it to accept any character. I think this could still work based on your original requirement of wanting to accept a string of only letters and hyphens. I'm curious, would you mind testing the updated code I included? I'd like to know if it works with the other fixed (also, note that I am not "notting" the preg_match) – adprocas Jul 26 '17 at 16:09