需要帮助来修复第 13 行 preg_match 语句中的正则表达式。
这是针对表单上的评论字段,我希望评论能够根据需要尽可能长,但他们应该只能使用字母、数字、感叹号、问号、逗号和句点,仅此而已。换句话说,一两个基本句子。
这是我的代码...
// Validate the message field is in regex format.
add_action( 'elementor_pro/forms/validation', function ( $record, $ajax_handler ) {
$fields = $record->get_field( [
'id' => 'message',
] );
if ( empty( $fields ) ) {
return;
}
$field = current( $fields );
if ( 1 !== preg_match( '/[^0-9a-z\s-]/i', $field['value'] ) ) {
$ajax_handler->add_error( $field['id'], 'Please write to us in English and no URLs allowed on this field.' );
}
}, 10, 2 );
在此先感谢您的帮助。
海梅
您的正则表达式匹配包含禁止字符的内容。所以你应该使用===
,而不是!==
。或者只是将其视为布尔值而不与任何内容进行比较。
您需要在正则表达式的允许字符列表中添加感叹号、问号、逗号和句点。
if (preg_match( '/[^!?,.0-9a-z\s-]/i', $field['value'] ) ) {
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句