【发布时间】:2018-01-09 11:44:18
【问题描述】:
我们在 WordPress 上使用 Gravity Forms 构建的 Web 表单对电话号码字段使用“单行文本”字段类型。
我怎样才能为此添加自定义验证,以便它只允许提交整数并且只有在有 10 个或更多输入时才允许提交?
这是因为在澳大利亚,我们的电话号码(不含国家代码)是 10 位数字。我们目前收到的回复包含字母或不完整的数字。
我刚刚实现了以下功能,但它不起作用。
// Form Phone Field Validation to use numbers only with minimum 10 digits
add_filter( 'gform_field_validation_16_4', 'custom_validation', 10, 4 ); //Replace the ## with your form ID
function custom_validation( $result, $value, $form, $field ) {
$value = str_replace(array('(',')','-',' '),'',$value);//Remove hyphens, parenthesis, and spaces - you can take this out if you want strict numbers only
if ( $result['is_valid'] && strlen( $value ) < 10 && is_numeric( $value ) ) {
$result['is_valid'] = false;
$result['message'] = 'Please enter a valid phone number of 10 or more digits with area code';
}
return $result;
}
【问题讨论】: