Mandeep Singh
Mandeep Singh

Reputation: 8234

libphonenumber node.js formats number to intl format even if it is invalid

I am using the libphonenumber library for node.js

In the google demo for this library, if I enter an invalid phone number, the intl format is not populated. But when I try to parse an invalid number via node.js library, it creates the intl string representation irrespective of whether the number is valid or not.

I need to make sure that only valid numbers are converted and invalids to return null.

As an example, consider this:

var phoneProto = phoneUtil.parse("12213", "IN");
phoneProto.intl = phoneUtil.format(phoneProto, 1);
sails.log.debug(phoneProto.intl);

Output

+91 12213

How can I make sure only valid ones are returned ?

Upvotes: 0

Views: 801

Answers (1)

pratZ
pratZ

Reputation: 3346

According to the library's documentation,

parse(java.lang.String numberToParse, java.lang.String defaultRegion)

Parses a string and returns it in proto buffer format. This method will throw a NumberParseException if the number is not considered to be a possible number. Note that validation of whether the number is actually a valid number for a particular region is not performed. This can be done separately with isValidNumber(PhoneNumber).

Upvotes: 1

Related Questions