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

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.