我正在尝试验证美国电话号码的字段,如果输入该字段的信息无效,则返回错误。尽管正则表达式似乎有效,但它目前不接受输入有效的任何信息:
add_action('register_form','myplugin_register_form2');
function myplugin_register_form2 (){
$phone_number = ( isset( $_POST['phone_number'] ) ) ? $_POST['phone_number']: '';
?>
<p id="phone_number">
<label for="phone_number"><?php _e('Phone Number <font size="1">(XXX XXX XXXX)</font>','mydomain') ?><br />
<input type="text" name="phone_number" id="phone_number" class="input" size="25" style="text-align:right" maxlength="14" />
</p>
<?php
}
//2. Phone_number Requirement
add_filter('registration_errors', 'myplugin_registration_errors2', 10, 3);
function myplugin_registration_errors2 ($errors, $sanitized_user_login, $user_email) {
$pattern = '/^(?:1(?:[. -])?)?(?:\((?=\d{3}\)))?([2-9]\d{2})(?:(?<=\(\d{3})\))? ?(?:(?<=\d{3})[.-])?([2-9]\d{2})[. -]?(\d{4})(?: (?i:ext)\.? ?(\d{1,5}))?$/';
if (!preg_match($pattern, $phone_number, $matches))
{
$errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') );
return $errors;
}
if ( empty( $_POST['phone_number'] ) )
$errors->add( 'phone_number_error', __('<strong>ERROR</strong>: You must include a valid phone number.','mydomain') );
return $errors;
}