0

在我的网站上,用户必须输入他们的电话号码才能通过短信验证和注册。在下面的函数中用户得到错误:

  1. 如果电话号码为空
  2. 如果电话号码存在(已注册)
  3. 如果电话号码少于 10 位

现在,如果他们在国家代码之前输入了 00 或 +,我想自动删除/忽略。(因此,如果他们输入 004798765432 或 +4798765432,那么正确的值将是:4798765432)。

如果输入的电话号码以 SINGLE 0 开头,我想显示一个不同的错误(因此,如果他们输入 02298765432,那么他们将收到错误消息,例如:您输入的电话号码不是国际电话号码)。

这是我正在使用的代码:

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;
}

/** 最后这个解决方案对我有用 */

/**
 * 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', __( '错误: 您输入的电话号码不是国际号码。', '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', __( '错误
(最少 10 位,不要使用“+”)','csnetworks'));}

        }
    }

    return $errors;
}
4

2 回答 2

0

使用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";
}

现场演示!

于 2013-08-26T18:56:11.937 回答
0

由于您在电话号码中有 +,这意味着它已经是一个字符串值。字符串只是字符数组,因此您可以这样访问它们。回声只是为了演示。

$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;
于 2013-08-26T19:09:05.253 回答