fbpx
Have questions? Call (+6) 011-2839 9361 (faisal) [email protected]

Gravity Forms: Get Validation Key From Text File (.txt)

We had an experience with one of our clients who need their form to be validated with a list of serial number. Their main purpose is to make sure that the only people who eligible to register with their form are their members. This method is not the best method we had (the best way is to put that serial number list into mySQL database, but we don’t know how to do that.. so if you know, let us know in the comment section below ;p). Here is the code :

Gravity Form Validation From File


<script type="text/javascript">

// FOR USER GROUP 1

add_filter('gform_validation_17', 'validate_code');
function validate_code($validation_result){

        if(!is_code_valid($_POST['input_40'])){
        $validation_result['is_valid'] = false;
        foreach($validation_result['form']['fields'] as &$field){
            // field 1 is the field where we want to show the validation message
            if($field['id'] == 40){
                $field['failed_validation'] = true;
                                $field['validation_message'] = 'The code you entered is invalid: please try again.';
                break;
            }
        }
    }
    return $validation_result;
}
 
// use this function to validate codes
function is_code_valid($thiscode){
        $codes = file('http://xxxxx.com/group-user-1.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach($codes as $code){
                if($thiscode == $code){
                        return TRUE;
                }
        }
        return FALSE;
}


// FOR USER GROUP 2

add_filter('gform_validation_18', 'validate_code2');
function validate_code2($validation_result2){

        if(!is_code_valid2($_POST['input_40'])){
        $validation_result2['is_valid'] = false;
        foreach($validation_result2['form']['fields'] as &$field){
            if($field['id'] == 40){
                $field['failed_validation'] = true;
                                $field['validation_message'] = 'The code you entered is invalid: please try again.';
                break;
            }
        }
    }
    return $validation_result2;
}
function is_code_valid2($thiscode2){
        $codes = file('http://xxxxxx.com/group-user-2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach($codes as $code2){
                if($thiscode2 == $code2){
                        return TRUE;
                }
        }
        return FALSE;
}

</script>

Pin It on Pinterest

Need Website? Lets Chat!