0

I'm attempting to validate a field to ensure it is 9 numerical digits (Canadian SIN number format). I've got the below filter set up:

add_filter( 'gform_field_validation_1_1', 'validate_SIN', 10, 4 );
   function validate_SIN( $result, $value, $form, $field ) {
    $pattern = "^\d{3}-?\d{3}-?\d{3}$";
    if ( !preg_match( $pattern, $value ) ) {
        $result['is_valid'] = false;
        $result['message']  = 'value:'.$value.' pattern:'.$pattern.' No Pattern Match';
    }

    return $result;
}

Invalid entries fail validation, but valid entries ALSO fail validation. I know the Regex is fine because I've used it before, so I'm at a loss as to why it's not working.

Does anyone have any guidance?

0

Probably you just forgot the delimiters for your pattern.

Try declaring your $pattern like:

$pattern = "/^\d{3}-?\d{3}-?\d{3}$/";
  • Amazing. Thank you kind stranger. Completely forgot the delimiters. – r3lai May 25 at 23:54
  • @r3lai I'm glad I could help. Accept my answer, please. – filipecsweb May 26 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.