-1

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;
}
Helena
  • 13
  • 7
  • Try to use trim on the input field or try another regexp. – Lorenz Jun 22 '13 at 11:08
  • http://stackoverflow.com/questions/3357675/validating-us-phone-number-with-php-regex There is a solution – Lorenz Jun 22 '13 at 11:11
  • Thanks Aragon0, but when using the above regex and adapting the final bits of code to match the function I am seeking ` foreach($aNumbers as $sNumber) { if (!preg_match($sPattern, $phone_number, $aMatches)) { $errors->add( 'phone_number_error', __('ERROR: You must include a valid phone number.','mydomain') ); return $errors; } }' it does not work. It keeps telling me the number is invalid. – Helena Jun 22 '13 at 11:28
  • Try your RegEx at http://www.functions-online.com/preg_match.html. – Lorenz Jun 22 '13 at 11:41
  • ERROR: missing ) at offset 1639 - this is using the regex as given in the post you referenced. I have not changed anything. – Helena Jun 22 '13 at 12:05

1 Answers1

0

OK, I found one that throws no errors:

/^(?:(?:(?:1[.\/\s-]?)(?!\())?(?:\((?=\d{3}\)))?((?(?!(37|96))[2-9][0-8][0-9](?<!(11)))?[2-9])(?:\((?<=\(\d{3}))?)?[.\/\s-]?([0-9]{2}(?<!(11)))[.\/\s-]?([0-9]{4}(?<!(555(01([0-9][0-9])|1212))))(?:[\s]*(?:(?:x|ext|extn|ex)[.:]*|extension[:]?)?[\s]*(\d+))?$/

But you have to strip all not-numbers first:

$input_number=preg_replace("[^0-9]","",$input_number);

With that you can also format your number as you want:

preg_match($pattern,$input_number);
$number=$result[1] . "-" . $result[4] . "-" . $result[6];

Another way is to set the input mask with jQuery as described in the thread I mentioned above. Then the user can only input a valid phone number.

I didn't find a good regexp that matches many formats.

Lorenz
  • 2,179
  • 3
  • 19
  • 18