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.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
*/
require(['N/format/i18n'], function(format) {
var origNumberStr = "7524105210ext.154";
var pnParser = format.getPhoneNumberParser({
defaultCountry: format.Country.UNITED_STATES
});
var phoneNumber = pnParser.parse({
number: origNumberStr
});
/* after parsing:
phoneNumber.countryCode is '1'
phoneNumber.extension is '154'
phoneNumber.nationalNumber is '7524105210'
phoneNumber.numberofLeadingZeros is 1
phoneNumber.carrierCode is ' '
phoneNumber.rawInput is '7524105210ext.154'
*/
var pnFormatter = format.getPhoneNumberFormatter({
formatType: format.PhoneNumberFormatType.NATIONAL
});
var strNumber = pnFormatter.format({
number: phoneNumber
}); // strNumber is '(752) 410-5210 ext. 154'
});
Related Topics
- N/format/i18n Module Script Samples
- Format 12345 as a German String
- Format a Number as a String Using N/format/i18n
- Format Numbers as Currency Strings
- Parse a Czech Phone Number
- Format Numbers Based on the Locale Parameter
- Format Currency Based on the Locale Parameter
- Format Numbers and Currencies Based on the English-India Locale Parameter
- SuiteScript 2.x Modules
- SuiteScript 2.x