Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contact Form: Improve phone no formatting #37603

Merged
merged 4 commits into from
Nov 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -15,7 +15,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 @@ -42,7 +42,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 @@ -163,11 +163,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