Age verification

This documentation page is for Quform version 1 and may not be applicable for Quform 2 click here to visit the documentation for Quform 2.

This guide will show you how to create a custom validator to verify the age of the person submitting the form.

Step 1

Add a Date element to your form.

Step 2

Get your unique element ID for your Date element, see Finding the unique element ID.

Step 3

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
3
4
5
6
7
8
9
10
11
12
13
1415
16
17
18
19
20
21
22
function my_verify_age($valid, $value, $element)
{
    if ($valid) {
        $age = get_age("{$value['year']}-{$value['month']}-{$value['day']}");
 
        if ($age < 18) {
            $element->addError('You need to be over 18');
            $valid = false;
        }
    }
 
    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_1', 'my_verify_age', 10, 3); 
// Get a person's age from date of birth
function get_age($dob)
{
    $adjust = (date("md") >= date("md", strtotime($dob))) ? 0 : -1;
    $years = date("Y") - date("Y", strtotime($dob));
    return $years + $adjust;
}
function my_verify_age($valid, $value, $element)
{
    if ($valid) {
        $age = get_age("{$value['year']}-{$value['month']}-{$value['day']}");

        if ($age < 18) {
            $element->addError('You need to be over 18');
            $valid = false;
        }
    }

    return $valid;
}
add_filter('iphorm_element_valid_iphorm_2_1', 'my_verify_age', 10, 3);

// Get a person's age from date of birth
function get_age($dob)
{
    $adjust = (date("md") >= date("md", strtotime($dob))) ? 0 : -1;
    $years = date("Y") - date("Y", strtotime($dob));
    return $years + $adjust;
}
  • On line 1, change iphorm_2_1 to your Date element unique ID obtained in step 2.

The above example checks that the form submitter is over 18, but you can change the number 18 to suit your needs, if you need to check if they are less than an age, change the less than sign < to a greater than sign > on the line containing the code if ($age < 18) {.

Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy