Skip to content

Parsing

Parse user input or stored values into an NF.Phone instance. Parsing does not guarantee the number is valid; call NF.isValid separately.

NF.parse

Parses a phone number string into a new NF.Phone with country code and national number set.

Signatures

global static NF.Phone parse(String rawNumber, String defaultRegion)

Parameters

NameTypeDescription
rawNumberStringText from a field or user input. May include spaces, dashes, parentheses, and a leading +.
defaultRegionStringISO 3166-1 alpha-2 region code (e.g. US, IT, DE) used when rawNumber has no country calling code.

Returns

NF.Phone with parsed fields populated. Validity is not checked.

Throws

NF.ParseException if the string cannot be parsed.

libphonenumber

PhoneNumberUtil.parse(CharSequence, String)

Example

// National-format Italian mobile on a Contact before insert
NF.Phone phone = NF.parse(contact.MobilePhone, 'IT');
// phone.getCountryCode() → 39
// phone.getNationalNumber() → 3123456789 (example)

NF.parseAndKeepRawInput

Like NF.parse, but also stores the original input and how the country code was detected.

Signatures

global static NF.Phone parseAndKeepRawInput(String rawNumber, String defaultRegion)

Parameters

NameTypeDescription
rawNumberStringOriginal phone text.
defaultRegionStringDefault region when no + country code is present.

Returns

NF.Phone with rawInput and countryCodeSource available via getters.

Throws

NF.ParseException if the string cannot be parsed.

libphonenumber

PhoneNumberUtil.parseAndKeepRawInput(CharSequence, String)

Example

NF.Phone phone = NF.parseAndKeepRawInput('+1 650-253-0000', 'US');
// phone.getRawInput() → "+1 650-253-0000"
// phone.getCountryCodeSource() → NF.CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN

NF.parseInto

Parses into an existing NF.Phone instance (mutates the target).

Signatures

global static NF.Phone parseInto(String rawNumber, String defaultRegion, NF.Phone target)

Parameters

NameTypeDescription
rawNumberStringText to parse.
defaultRegionStringDefault region code.
targetNF.PhoneInstance to fill. If null, a new NF.Phone is created.

Returns

The same NF.Phone instance that was parsed into (never null).

Throws

NF.ParseException if the string cannot be parsed.

libphonenumber

PhoneNumberUtil.parse(CharSequence, String, PhoneNumber)

Example

NF.Phone reusable = new NF.Phone();
NF.parseInto('044 668 18 00', 'CH', reusable);
// reusable.getCountryCode() → 41
// reusable.getNationalNumber() → 446681800

NF.parseAndKeepRawInputInto

Combines NF.parseInto with raw-input preservation.

Signatures

global static NF.Phone parseAndKeepRawInputInto(String rawNumber, String defaultRegion, NF.Phone target)

Parameters

NameTypeDescription
rawNumberStringText to parse.
defaultRegionStringDefault region code.
targetNF.PhoneInstance to fill, or null to allocate a new one.

Returns

The populated NF.Phone instance.

Throws

NF.ParseException if the string cannot be parsed.

libphonenumber

PhoneNumberUtil.parseAndKeepRawInput(CharSequence, String, PhoneNumber)

Example

NF.Phone phone = NF.parseAndKeepRawInputInto(lead.Phone, 'US', null);
// phone.getRawInput() → original lead.Phone value

NF.normalizeDigitsOnly

Strips a string down to Unicode decimal digits only.

Signatures

global static String normalizeDigitsOnly(String value)

Parameters

NameTypeDescription
valueStringArbitrary text that may contain digits and separators.

Returns

Digits-only string. May be empty if no digits were found.

Throws

None.

libphonenumber

PhoneNumberUtil.normalizeDigitsOnly(String)

Example

String digits = NF.normalizeDigitsOnly('+1 (650) 253-0000');
// digits → "16502530000"

NF.normalizeDiallableCharsOnly

Keeps only characters valid in a dialable phone string (digits and dial pauses/waits).

Signatures

global static String normalizeDiallableCharsOnly(String value)

Parameters

NameTypeDescription
valueStringRaw dial string.

Returns

Normalized dialable string.

Throws

None.

libphonenumber

PhoneNumberUtil.normalizeDiallableCharsOnly(String)

Example

String dialable = NF.normalizeDiallableCharsOnly('650-253-0000;ext=123');

NF.convertAlphaCharactersInNumber

Converts letters on a phone keypad to digits (e.g. vanity numbers).

Signatures

global static String convertAlphaCharactersInNumber(String value)

Parameters

NameTypeDescription
valueStringString that may contain A–Z letters.

Returns

String with alphabetic characters mapped to digits.

Throws

None.

libphonenumber

PhoneNumberUtil.convertAlphaCharactersInNumber(String)

Example

String digits = NF.convertAlphaCharactersInNumber('1800 MICROSOFT');
// digits → "18006427678"
NF.Phone vanity = NF.parseAndKeepRawInput('1800 MICROSOFT', 'US');