简体   繁体   English

如何使 Gravity Forms 名称字段输入只接受字母

[英]How to make Gravity Forms name field input only accept letters

如何使重力表单名称字段只接受字母,数字应该会出现错误。

You need to use gform_field_validation validation filter to be able to do this kind of validation before the form submits.您需要使用gform_field_validation验证过滤器才能在表单提交之前进行这种validation

In addition, we need to use preg_match function of PHP with a regex to ensure we are only taking Letters from Full Name input value.此外,我们需要使用带有regex的 PHP 的preg_match函数来确保我们只从全名输入值中获取字母

Just add this code in your active theme functions.php file: (Code tested and working)只需将此代码添加到您的活动主题functions.php 文件中即可:(代码已测试且有效)

add_filter( 'gform_field_validation', function ( $result, $value, $form, $field ) {
    if ( $field->type == 'name' ) {

        // Input values
        $fullName = rgar( $value, $field->id . '.3' );
         
        if ( empty( $fullName )) {
            $result['is_valid'] = false;
            $result['message']  = empty( $field->errorMessage ) ? __( 'This field is required. Please enter a complete name.', 'gravityforms' ) : $field->errorMessage;
        } else if (preg_match('/[A-Za-z].*[0-9]|[0-9].*[A-Za-z]/', $fullName)) { //check for letters only
            $result['is_valid'] = false;
            $result['message']  = empty( $field->errorMessage ) ? __( 'Full name must ony contains letters.', 'gravityforms' ) : $field->errorMessage;
        } else {
            $result['is_valid'] = true;
            $result['message']  = '';
        }
    }
    return $result; //return results
}, 10, 4 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM