php - 使用正则表达式验证 wordpress 字段
问题描述
我使用 functions.php 中的代码在注册表单中创建了一个新字段:
function wooc_extra_register_fields() {?>
<p class="form-row form-row-wide">
<label for="reg_billing_first_name"><?php _e( 'Bitcoin Refund Address', 'woocommerce' ); ?><span class="required">*</span></label>
<input type="text" class="input-text" name="billing_first_name" id="reg_billing_first_name" value="<?php if ( ! empty( $_POST['billing_first_name'] ) ) esc_attr_e( $_POST['billing_first_name'] ); ?>" />
</p>
<div class="clear"></div>
<?php
}
add_action( 'woocommerce_register_form_start', 'wooc_extra_register_fields' );
现在我需要使用正则表达式验证该字段,如下所示:
^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$
当前字段验证不能为空。如果有人可以提供帮助,我将不胜感激
function wooc_validate_extra_register_fields( $username, $email, $validation_errors ) {
if ( isset( $_POST['billing_first_name'] ) && empty( $_POST['billing_first_name'] ) ) {
$validation_errors->add( 'billing_first_name_error', __( 'Refund Address is required!', 'woocommerce' ) );
}
return $validation_errors;
}
add_action( 'woocommerce_register_post', 'wooc_validate_extra_register_fields', 10, 3 );
解决方案
使用preg_match
:
if ( preg_match( '/^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/', $_POST['billing_first_name'] ) ) {...}
推荐阅读
- laravel-7 - 无法安装 cviebrock/eloquent-sluggable
- c# - 无法在 Exited eventhadler 中捕获进程名称和退出代码
- .net - 获取 PEM 公钥和私钥或从 PFX 文件
- reactjs - 获取“JSX 元素类型‘App’没有任何构造或调用签名。” 导出 JSX 元素数组时出现 ts(2604) 错误
- reactjs - FluentUI/react-northstar 中带有项目 ID 的下拉菜单
- python - 基于条件的索引范围
- email - Modoboa 服务器无法处理外部电子邮件(如 Google、Yahoo)
- linux - 使用 SED 从文本文件中的值生成特定格式(逐行)
- c# - 如何在 C# 中使用 gettext 复数定义函数?
- c# - c# sql server更新表连接超时