I'm attempting to validate a field for US phone numbers and return an error if the information entered into the field is not valid. It is currently not accepting any information as entered as valid even though the regex seems valid:
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;
}