Gravity Forms and regex - Doesn't seem to validate

I'm attempting to validate a field to ensure it is 9 numerical digits (Canadian SIN number format). I've got t

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?

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?

Share Improve this question edited May 25, 2019 at 23:06 fuxia 107k39 gold badges255 silver badges459 bronze badges asked May 25, 2019 at 22:54 r3lair3lai 33 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Probably you just forgot the delimiters for your pattern.

Try declaring your $pattern like:

$pattern = "/^\d{3}-?\d{3}-?\d{3}$/";

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745463961a4628833.html

相关推荐

  • Gravity Forms and regex - Doesn't seem to validate

    I'm attempting to validate a field to ensure it is 9 numerical digits (Canadian SIN number format). I've got t

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信