Updated phone validation and formatting rules.

This commit is contained in:
Peter Fabian 2018-08-24 15:43:46 +02:00
parent 53f58eb0d5
commit 897af8d20e
6 changed files with 7 additions and 7 deletions

View File

@ -699,8 +699,6 @@ class WC_Checkout {
}
if ( in_array( 'phone', $format, true ) ) {
$data[ $key ] = wc_format_phone_number( $data[ $key ] );
if ( $validate_fieldset && '' !== $data[ $key ] && ! WC_Validation::is_phone( $data[ $key ] ) ) {
/* translators: %s: phone number */
$errors->add( 'validation', sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . esc_html( $field_label ) . '</strong>' ) );

View File

@ -130,8 +130,6 @@ class WC_Form_Handler {
}
break;
case 'phone' :
$_POST[ $key ] = wc_format_phone_number( $_POST[ $key ] );
if ( ! WC_Validation::is_phone( $_POST[ $key ] ) ) {
wc_add_notice( sprintf( __( '%s is not a valid phone number.', 'woocommerce' ), '<strong>' . $field['label'] . '</strong>' ), 'error' );
}

View File

@ -30,7 +30,7 @@ class WC_Validation {
* @return bool
*/
public static function is_phone( $phone ) {
if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)]/', '', $phone ) ) ) ) {
if ( 0 < strlen( trim( preg_replace( '/[\s\#0-9_\-\+\/\(\)\.]/', '', $phone ) ) ) ) {
return false;
}

View File

@ -941,6 +941,9 @@ function wc_normalize_postcode( $postcode ) {
* @return string
*/
function wc_format_phone_number( $phone ) {
if ( ! WC_Validation::is_phone( $phone ) ) {
return '';
}
return preg_replace( '/[^0-9\+\-\s]/', '-', preg_replace( '/[\x00-\x1F\x7F-\xFF]/', '', $phone ) );
}

View File

@ -761,8 +761,9 @@ class WC_Tests_Formatting_Functions extends WC_Unit_Test_Case {
*/
public function test_wc_format_phone_number() {
$this->assertEquals( '1-610-385-0000', wc_format_phone_number( '1.610.385.0000' ) );
$this->assertEquals( '+47 0000 00003', wc_format_phone_number( '+47 0000 00003' ) ); // This number contains non-visible unicode chars at the beginning and end of string, should remove all those.
$this->assertEquals( '', wc_format_phone_number( '+47 0000 00003' ) ); // This number contains non-visible unicode chars at the beginning and end of string, should remove all those.
$this->assertEquals( '27 00 00 0000', wc_format_phone_number( '27 00 00 0000' ) );
$this->assertEquals( '', wc_format_phone_number( '1-800-not a phone number' ) );
}
/**

View File

@ -27,7 +27,7 @@ class WC_Tests_Validation extends WC_Unit_Test_Case {
array( true, WC_Validation::is_phone( '+00 000 00 00 000' ) ),
array( true, WC_Validation::is_phone( '+00-000-00-00-000' ) ),
array( true, WC_Validation::is_phone( '(000) 00 00 000' ) ),
array( false, WC_Validation::is_phone( '+00.000.00.00.000' ) ),
array( true, WC_Validation::is_phone( '+00.000.00.00.000' ) ),
array( false, WC_Validation::is_phone( '+00 aaa dd ee fff' ) ),
);
}