Skip to content

Commit

Permalink
Contact Form: Improve phone no formatting (#37603)
Browse files Browse the repository at this point in the history
Improvement of formatting code of the phone input in the contact form, fixing a few annoying issues, and increasing its stability in general
  • Loading branch information
eltongo authored Nov 14, 2019
1 parent 637b00a commit 848216e
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions client/components/phone-input/phone-number.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const debug = debugFactory( 'phone-input:metadata' );

export const DIGIT_PLACEHOLDER = '\u7003';
const STANDALONE_DIGIT_PATTERN = /\d(?=[^,}][^,}])/g;
const CHARACTER_CLASS_PATTERN = /\[([^\[\]])*\]/g;
const CHARACTER_CLASS_PATTERN = /\[([^[\]])*]/g;
const LONGEST_NUMBER = '999999999999999';
const LONGEST_NUMBER_MATCH = /9/g;
export const MIN_LENGTH_TO_FORMAT = 3;
Expand All @@ -40,7 +40,7 @@ export function findCountryFromNumber( inputNumber ) {
const query = stripNonDigits( inputNumber )
.replace( /^0+/, '' )
.substr( 0, i );
if ( dialCodeMap.hasOwnProperty( query ) ) {
if ( Object.prototype.hasOwnProperty.call( dialCodeMap, query ) ) {
const exactMatch = dialCodeMap[ query ];
if ( exactMatch.length === 1 ) {
return countries[ exactMatch[ 0 ] ];
Expand Down Expand Up @@ -161,11 +161,15 @@ export function applyTemplate( phoneNumber, template, positionTracking = { pos:
*/
export function processNumber( inputNumber, numberRegion ) {
let prefix = numberRegion.nationalPrefix || '';
const nationalNumber = stripNonDigits( inputNumber ).replace(
new RegExp( '^(' + numberRegion.dialCode + ')?(' + numberRegion.nationalPrefix + ')?' ),
let nationalNumber = stripNonDigits( inputNumber ).replace(
new RegExp( '^(0*' + numberRegion.dialCode + ')?(' + numberRegion.nationalPrefix + ')?' ),
''
);

if ( numberRegion.nationalPrefix === '0' ) {
nationalNumber = nationalNumber.replace( /^0+/, '' );
}

debug( `National Number: ${ nationalNumber } for ${ inputNumber } in ${ numberRegion.isoCode }` );

if ( inputNumber[ 0 ] === '+' ) {
Expand Down

0 comments on commit 848216e

Please sign in to comment.