On my site users must enter their phone number to get Verified and registered by SMS. In the function below users get ERROR:

  1. If the phone number is empty
  2. If the phone number exists (already registered)
  3. If the phone number is less then 10 digits

Now I want to AUTO REMOVE/IGNORE if they have entered 00 or + before country code. (So if they enter 004798765432 or +4798765432 then the right value will be: 4798765432).

And I want to show a DIFFERENT ERROR if the entered phone number starts with a SINGLE 0 (So if they enter 02298765432 then they will get ERROR msg like: Phone number you entered is not a international phone number).

Here is the code that I'm using:

function registration_errors( $errors ) {
    $enabled = csnetworks_sms_get_option( 'register_form' );

    if ( $enabled == 'on' ) {
        $phone = $_POST['user_phone'];

        if ( $phone == '' ) {
            $errors->add( 'empty_phone', __( '<strong>ERROR</strong>: Please type your phone number.', 'csnetworks' ) );
        } else if ( phone_exists( $phone ) ) {
            $errors->add( 'phone_exists', __( '<strong>ERROR</strong>: Phone number is already registered.', 'csnetworks' ) );
        } else {
            if ( preg_match( '/[^\d]/', $phone ) || (strlen( $phone ) < 10 ) ) {
                $errors->add( 'invalid_phone', __( '<strong>ERROR</strong>: Please type a valid phone number (10 digit min.)', 'csnetworks' ) );
            }
        }
    }
    return $errors;
}

/** Finally this solution worked for me */

/**
 * Validates phone number
 *
 * @param type $errors
 * @return type
 */

function registration_errors( $errors ) {
    $enabled = csnetworks_sms_get_option( 'register_form' );

    if ( $enabled == 'on' ) {
        $phone = $_POST['user_phone'];

if ($phone[1] == '0') $phone = substr($phone, 2);

if ($phone[0] == '0') { $errors->add( 'invalid_inter_phone', __( 'ERROR: Phone Number you entered is NOT a international number.', 'csnetworks' ) ); } if ( $phone == '' ) { $errors->add( 'empty_phone', __( 'ERROR: Please type your phone number.', 'csnetworks' ) ); } else if ( phone_exists( $phone ) ) { $errors->add( 'phone_exists', __( 'ERROR: Phone number is already registered.', 'csnetworks' ) ); } else { if ( preg_match( '/[^\d]/', $phone ) || (strlen( $phone ) < 10 ) ) { $errors->add( 'invalid_phone', __( 'ERROR: Please type a valid Phone Number
(10 digit minimum and do NOT use "+")', 'csnetworks' ) ); }

        }
    }

    return $errors;
}
有帮助吗?

解决方案

Since you have + in phone number that means it's already a string value. String is just array of chars, so you can access them as such. echo's are there just for demonstration.

$phone = '0479876543';

if($phone[0] == '+')
    $phone = substr($phone, 1);
elseif($phone[0] == '0')
{
    if($phone[1] == '0')
        $phone = substr($phone, 2);     
    else{
        echo 'Phone number you entered is not a international phone number.';
    }
}


echo $phone;

其他提示

Use subtr():

if (substr($phone, 0, 2) == '00') {
    $phone =  substr($phone, 2);
}
elseif (substr($phone, 0, 1) == '+') {
    $phone = substr($phone, 1);
}
elseif (substr($phone, 0, 1) == '0') {
    echo "Phone number you entered is not a international phone number";
}

Live demo!

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top