bp_xprofile_validate_phone_value( $retval,  $field_id,  $value, null $user_id = null )

Validate phone number format.

Description

Parameters

$retval

(Required)

$field_id

(Required)

$value

(Required)

$user_id

(Optional)

Default value: null

Return

(string)

Source

File: bp-xprofile/bp-xprofile-filters.php

839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
function bp_xprofile_validate_phone_value( $retval, $field_id, $value, $user_id = null ) {
 
    if ( 'telephone' !== xprofile_get_field( $field_id)->type ) {
        return $retval;
    }
 
    if ( $retval ) {
        return $retval;
    }
 
    if ( false === xprofile_check_is_required_field( $field_id ) && '' === $value ) {
        return $retval;
    }
 
    $international = false;
    $selected_format = bp_xprofile_get_meta( $field_id, 'field', 'phone_format', true );
    if ( empty( $selected_format ) ) {
        $international = true;
    }
 
    $str = trim( $value );
    $str = preg_replace( '/\s+(#|x|ext(ension)?)\.?:?\s*(\d+)/', ' ext \3', $str );
 
    $us_number = preg_match( '/^(\+\s*)?((0{0,2}1{1,3}[^\d]+)?\(?\s*([2-9][0-9]{2})\s*[^\d]?\s*([2-9][0-9]{2})\s*[^\d]?\s*([\d]{4})){1}(\s*([[:alpha:]#][^\d]*\d.*))?$/', $str, $matches );
 
    $field_name = xprofile_get_field( $field_id )->name;
 
    if ( empty( $str) ) {
        /* SET ERROR: The field must be a valid U.S. phone number (e.g. 888-888-8888) */
        return sprintf( __( '%s is required and not allowed to be empty.', 'buddyboss' ), $field_name );
    }
 
    if ( $us_number ) {
        return $retval;
    }
 
    if ( ! $international ) {
        /* SET ERROR: The field must be a valid U.S. phone number (e.g. 888-888-8888) */
        return sprintf( __( 'Enter valid %s', 'buddyboss' ), $field_name );
    }
 
    $valid_number = preg_match( '/^(\+\s*)?(?=([.,\s()-]*\d){8})([\d(][\d.,\s()-]*)([[:alpha:]#][^\d]*\d.*)?$/', $str, $matches ) && preg_match( '/\d{2}/', $str );
 
    if ( $valid_number ) {
        return $retval;
    }
 
    /* SET ERROR: The field must be a valid phone number (e.g. 888-888-8888) */
    return sprintf( __( 'Enter valid %s', 'buddyboss' ), $field_name );
}

Changelog

Changelog
Version Description
BuddyBoss 1.0.0 Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.