On my site users must enter their phone number to get Verified and registered by SMS.
In the function below users get ERROR:
- If the phone number is empty
- If the phone number exists (already registered)
- 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;
}