Skip to content

Commit

Permalink
Contact form: Fix problem when inserting phone number that begins wit…
Browse files Browse the repository at this point in the history
…h same digits as country code (#54477)

The phone input field in the domain contact information form has had a problem for some time (issue #25039): when a user tries to input a phone number which begins with the same digits as the selected country's international dialing code, these numbers are removed from the input. Copy and pasting also doesn't work, the initial digits are removed the same.

For example, Brazil's dial code is +55, and a valid Brazilian phone number is 55 1234-5678, 55 being a national city code. However, you can't input that number because the 55 is constantly removed.

This PR fixes that behavior by updating the logic of when the international dialing prefix should be removed.

Commits:

* Add unit tests that reproduce issue #25039

When a user tries to input a phone number which starts with the same
digits as the selected country's code, that initial part gets removed.
This happens for example for Brazilian (international code +55) phone
numbers: if I write 55 12345678 (55 being a valid city code), it gets
truncated into 12345678. As reported in issue #25039, this happens for
any phone number in the same situation.

* Update test-phone-number with some more cases

* Update logic to remove dial code prefixes

* Add some more test cases
  • Loading branch information
leonardost authored Jul 14, 2021
1 parent 0bfee7b commit 8ab4c5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
19 changes: 15 additions & 4 deletions client/components/phone-input/phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,21 @@ export function applyTemplate( phoneNumber, template, positionTracking = { pos:
*/
export function processNumber( inputNumber, numberRegion ) {
let prefix = numberRegion.nationalPrefix || '';
let nationalNumber = stripNonDigits( inputNumber ).replace(
new RegExp( '^(0*' + numberRegion.dialCode + ')?(' + numberRegion.nationalPrefix + ')?' ),
''
);

let nationalNumber = stripNonDigits( inputNumber );
// If the number starts with a '+', then it most likely starts with an international dialing code
// that should be removed. Otherwise, the prefix is probably part of the national number.
// NANPA countries (with dial code or country dial code '1') also should have the '1' removed here.
if (
inputNumber[ 0 ] === '+' ||
numberRegion.dialCode === '1' ||
numberRegion.countryDialCode === '1'
) {
nationalNumber = nationalNumber.replace(
new RegExp( '^(0*' + numberRegion.dialCode + ')?(' + numberRegion.nationalPrefix + ')?' ),
''
);
}

if ( numberRegion.nationalPrefix === '0' ) {
nationalNumber = nationalNumber.replace( /^0+/, '' );
Expand Down
10 changes: 10 additions & 0 deletions client/components/phone-input/test/test-phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ describe( 'metadata:', () => {
test( 'should format full length numbers', () => {
equal( formatNumber( '+14252222222', countries.US ), '+1 425-222-2222' );
equal( formatNumber( '+905325555555', countries.TR ), '+90 532 555 55 55' );
equal( formatNumber( '+5555912345678', countries.BR ), '+55 55 91234-5678' );
equal( formatNumber( '+393911711711', countries.IT ), '+39 391 171 1711' );
equal( formatNumber( '+919100123456', countries.IN ), '+91 91001 23456' );
} );

test( 'should format as you type', () => {
Expand Down Expand Up @@ -188,6 +191,10 @@ describe( 'metadata:', () => {
equal( formatNumber( '4252222222', countries.US ), '(425) 222-2222' );
equal( formatNumber( '05325555555', countries.TR ), '0532 555 55 55' );
equal( formatNumber( '0215369851', countries.AU ), '02 1536 9851' );
equal( formatNumber( '3911711711', countries.IT ), '391 171 1711' );
equal( formatNumber( '5512345678', countries.BR ), '055 1234-5678' );
equal( formatNumber( '9100123456', countries.IN ), '091001 23456' );
equal( formatNumber( '00000123456', countries.UK ), '01234 56' );
} );

test( 'should format as you type', () => {
Expand Down Expand Up @@ -274,6 +281,9 @@ describe( 'metadata:', () => {
test( 'should separate country codes properly for countries with +1 and a separate leading digit', () => {
equal( toIcannFormat( '+18686559999', countries.TT ), '+1.8686559999' );
} );
test( 'should be able to handle other countries', () => {
equal( toIcannFormat( '+555512345678', countries.BR ), '+55.5512345678' );
} );
} );
} );

Expand Down

0 comments on commit 8ab4c5a

Please sign in to comment.