Parse a U.S. Phone Number
The following sample parses a U.S. phone number and logs the country code, extentions, national number, number of leading zerios, carrier code, and raw input.
Note:
This sample script uses the require function so you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (a script you attach to a script record and deploy). For more information, see SuiteScript 2.1 Script Basics and SuiteScript 2.1 Script Types.
/**
* @NApiVersion 2.1
*/
require(['N/format/i18n'], (format) => {
const formatPhoneNumber = () => {
const origNumberStr = "7524105210ext.154";
const pnParser = format.getPhoneNumberParser({
defaultCountry: format.Country.UNITED_STATES
});
const phoneNumber = pnParser.parse({
number: origNumberStr
});
const pnFormatter = format.getPhoneNumberFormatter({
formatType: format.PhoneNumberFormatType.NATIONAL
});
const strNumber = pnFormatter.format({
number: phoneNumber
});
return { phoneNumber, strNumber };
};
return { formatPhoneNumber };
});