我使用 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 );
uj5u.com熱心網友回復:
使用preg_match:
if ( preg_match( '/^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$/', $_POST['billing_first_name'] ) ) {...}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312709.html
標籤:php 正则表达式 WordPress的 验证 求购
上一篇:收藏視圖中的收藏品不可見
