Field for code entry with verification of code entered

Hi. Using only the available options, e.g., conditional logic, would it be possible to verify a “static” code entered in an appropriate field?

Let me explain: there will be 80 codes that will be made available; there will be a field in the form for entering a code; the field will be mandatory; the user will be able to continue and submit the form only if the code entered in the appropriate field is correct, i.e., one of the 80 “original” codes.

Can this be done only with the tools in GravityForms or will it be necessary to write code?

Many thanks for your interest. Best regards. Bruno

Or is there a GravityForms extension that allows me to do this?

I have a form ready, but I am stuck on validating the “CODICE VOUCHER” field, the last of the fields to be filled in: it has to be validated and I can’t.

A code - numbers and letters - must be entered in the field, which must correspond to one of 80 specially created codes consisting of numbers and letters.

If it would help, the 80 codes could have the same combination at the beginning or end. For example.

ABC123
ABC456
ABC789

maybe the solution here
https://docs.gravityforms.com/gform_field_validation/#h-number-field-validation

Thank you for your time. Best regards. Bruno

Hi,

Yes, you can use the gform_field_validation hook to validate the input field.

Here is quick code example

add_filter('gform_field_validation_1_5', function($result, $value, $form, $field) {
    // Define the valid codes
    $valid_codes = [
        'ABC123', 'ABC456', 'ABC789', // Add your codes here
    ];

    // Trim and check if the entered code is in the valid codes array
    if (!in_array(trim($value), $valid_codes, true)) {
        // If not valid, set the error message
        $result['is_valid'] = false;
        $result['message'] = 'Please enter a valid CODICE VOUCHER.';
    }

    return $result;
}, 10, 4);

gform_field_validation_1_5 … here replace 1 with your form id and 5 with CODICE VOUCHER field id.

Add this code in your sites functions.php file

WOW, amazing, thanks a lot Sushil Kumar, I will try your solution as soon as I can, with much pleasure_^
Best regards

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.