Comments (32)
getNumberType()
always returns phone number type with the full metadata for a county.
With the default metadata use parse()
instead.
from libphonenumber-js.
I use another option to solve it.
const formatter = new asYouType('ID');
formatter.input(value);
const valid = formatter.valid;
if(!valid){
return errorMessage;
}
thank you for your response
from libphonenumber-js.
That works too.
It's the same as parse(value, 'ID')
from libphonenumber-js.
Actually, there's a dedicated function for validating phone numbers: isValidNumber(value, 'ID')
from libphonenumber-js.
i got different result between isValidNumber and my previous solution for input = 1234567890.
when using isValidNumber, it returns true. Actually it should be false, right?
but I get the correct result when using my code above, it returns "false".
from libphonenumber-js.
Provide the exact code
from libphonenumber-js.
check this simple code,
value = 12345678901
const validate = (value) => {
const isValidFn = isValidNumber(value, 'ID'); //it will return true
const formatter = new asYouType('ID');
formatter.input(value);
const valid = formatter.valid; //it will return false
return undefined;
}
from libphonenumber-js.
my previous solution is worked to validate all Indonesia phone number,
but how if i want to validate and make sure the input value is a mobile number.
i have tried to use
getNumberType('087717199324', 'ID') === 'MOBILE'
it always returns undefined,. that's a correct Indonesia number
from libphonenumber-js.
I already explained that getNumberType
only works for full metadata which is not the default one
from libphonenumber-js.
I looked into the code you provided and turns out that "as you type" formatter was giving false negatives for .valid
property, i.e. when it said .valid === false
it just meant that it didn't find a phone number format for the phone number and didn't neccessary mean that the value
is invalid (because for some countries there might be no phone number format but the phone number might still be valid - those are rare cases but they exist).
Therefore I released 0.4.x
version where I removed the .valid
property completely.
As I said before, for more precise phone number validation use "full metadata" for desired countries.
from libphonenumber-js.
I tried to use your suggestion
It's the same as parse(value, 'ID')
but when I debug it, I get object value below:
{ country: 'ID', phone: '85867899XXX' }
could you please give me example to get the numberType?
as I said, I want to check whether my input value is a valid Indonesia Mobile Number or not.
Thank you,
from libphonenumber-js.
First compile custom metadata for ID
with the --extended
flag.
Next use either getNumberType()
or isValidNumber()
.
And that should be working.
from libphonenumber-js.
i have run the script to compile custom metadata
"libphonenumber-metadata": "libphonenumber-generate-metadata metadata.min.json --countries ID --extended"
then import and add custom function :
import { getNumberType } from 'libphonenumber-js';
import metadata from '../../metadata.min.json';
export const getNumberTypeCustom = (...args) => getNumberType(...args, metadata);
export const validate = errorKey => (value) => {
if (isEmpty(value)) return undefined;
const numberType = getNumberTypeCustom(value, 'ID');
console.log(numberType) //it return undefined
if (numberType !== 'MOBILE') {
return errorMessage;
}
return undefined;
};
numberType still return undefined for a valid mobile phone number (085867899XXX),
from libphonenumber-js.
https://github.com/halt-hammerzeit/libphonenumber-js#customizing-metadata
You're using the wrong exports.
The correct exports are the ones with Custom
postfix
from libphonenumber-js.
you mean, I should write this code ?
import { getNumberTypeCustom } from 'libphonenumber-js';
import metadata from '../../metadata.min.json';
export const getNumberType = (...args) => getNumberTypeCustom(...args, metadata); //error on this line
export const validate = errorMessage => (value) => {
if (isEmpty(value)) return undefined;
const numberType = getNumberType(value, 'ID');
console.log(numberType) //it return undefined
if (numberType !== 'MOBILE') {
return errorMessage;
}
return undefined;
};
it show an error.
TypeError: Cannot read property 'apply' of undefined
when i check index of library 'libphonenumber-js', it redirect to index.common.js, and there is no custom function exported.
check this one :
'use strict'
var custom = require('./custom')
var metadata = require('./metadata.min.json')
exports = module.exports = {}
exports.parse = function parse()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.parse.apply(this, parameters)
}
exports.format = function format()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.format.apply(this, parameters)
}
exports.get_number_type = function get_number_type()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.getNumberType.apply(this, parameters)
}
exports.is_valid_number = function is_valid_number()
{
var parameters = Array.prototype.slice.call(arguments)
parameters.push(metadata)
return custom.isValidNumber.apply(this, parameters)
}
exports.as_you_type = function as_you_type(country)
{
custom.asYouType.call(this, country, metadata)
}
exports.as_you_type.prototype = Object.create(custom.asYouType.prototype, {})
exports.as_you_type.prototype.constructor = exports.as_you_type
exports.DIGIT_PLACEHOLDER = custom.DIGIT_PLACEHOLDER
// camelCase aliases
exports.getNumberType = exports.get_number_type
exports.isValidNumber = exports.is_valid_number
exports.asYouType = exports.as_you_type
from libphonenumber-js.
You found a bug: import { getNumberTypeCustom } from 'libphonenumber-js'
was missing from the ES6 export.
I added it in the newest release.
Try it and report back.
from libphonenumber-js.
(alternatively one can use import { getNumberType } from 'libphonenumber-js/custom
to reduce the resulting bundle size if the ES6 bundler can't handle proper tree shaking which can be the case currently)
from libphonenumber-js.
have you updated the Readme or any documentation?
from libphonenumber-js.
Yes, I added a note on "tree-shaking" and the /custom
export
from libphonenumber-js.
still get the same result,
I have updated my package version, currently, I'm on this version:
"libphonenumber-js": "^0.4.2",
could you please try this code in your environment?
example input number: 085867899998
import { getNumberType as getNumberTypeCustom } from 'libphonenumber-js/custom';
export const getNumberType = (...args) => getNumberTypeCustom(...args, metadata);
export const validate = errorMessage => (number) => {
if (isEmpty(number)) return undefined;
const numberType = getNumberType(number, 'ID');
console.log(numberType) //return undefined
if (numberType !== 'MOBILE') {
return errorMessage;
}
return successMessage;
};
from libphonenumber-js.
If you make up a sample project with simple reproduction instructions I could try that
from libphonenumber-js.
Ok, I looked into this test case and turned out there was a minor bug in getNumberType()
which is fixed in the newest version.
from libphonenumber-js.
I have tried the latest version, but it is still not passed my test cases,
I created new simple project to simulate this validation,
you can clone my repository here:
https://github.com/andangrd/simplePhoneValidation
I also create readme file, just follow the steps and please take a look at my code in src folder.
from libphonenumber-js.
➜ simplePhoneValidation git:(master) npm run test
> [email protected] test /Users/kuchumovn/work/simplePhoneValidation
> NODE_ENV=production jest --runInBand
FAIL src/phoneValidation.test.js
● Console
console.log src/phoneValidation.js:8
numberType for 085867899444 : MOBILE
console.log src/phoneValidation.js:8
numberType for +6285867899444 : MOBILE
console.log src/phoneValidation.js:8
numberType for 1234567890 : undefined
console.log src/phoneValidation.js:8
numberType for 0219533553 : MOBILE
So it's working.
I didn't use the yarn
commands because I don't have it installed.
npm install && npm test
from libphonenumber-js.
FAIL src/phoneValidation.test.js
it said "failed."
last printed test case should be undefined or other than MOBILE, because it is a fixed line number / landline number, not a mobile number
from libphonenumber-js.
numberType for 085867899444 : MOBILE
Which means it's working.
What exactly do you think isn't working.
from libphonenumber-js.
I mean this one:
console.log src/phoneValidation.js:8
numberType for 0219533553 : MOBILE
when you open phoneValidation.js, you will see these test cases :
describe('FormValidation', () => {
test('phoneValidation', () => {
expect(phoneValidation(ERROR_MESSAGE)('085867899444')).toBe(SUCCESS); //valid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('+6285867899444')).toBe(SUCCESS); //valid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('1234567890')).toBe(ERROR_MESSAGE); //invalid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('0219533553')).toBe(ERROR_MESSAGE); //valid landline number but should be an invalid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('+6221533553')).toBe(ERROR_MESSAGE); //valid landline number but should be an invalid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('+92421533553')).toBe(ERROR_MESSAGE); //invalid mobile phone number
expect(phoneValidation(ERROR_MESSAGE)('4215335536')).toBe(ERROR_MESSAGE); //invalid mobile phone number
});
});
from libphonenumber-js.
So which exact phone numbers you think aren't working and what is the exact expected result
from libphonenumber-js.
The one you said about is undetermined:
https://libphonenumber.appspot.com/phonenumberparser?number=0219533553&country=ID
FIXED_LINE_OR_MOBILE
from libphonenumber-js.
ah, ya..I think it will success and pass the test case when the value of getNumberType() is FIXED_LINE_OR_MOBILE.
but if you check your test result, it return "MOBILE" for this number 0219533553
FAIL src/phoneValidation.test.js
● Console
console.log src/phoneValidation.js:8
numberType for 085867899444 : MOBILE
console.log src/phoneValidation.js:8
numberType for +6285867899444 : MOBILE
console.log src/phoneValidation.js:8
numberType for 1234567890 : undefined
console.log src/phoneValidation.js:8
numberType for 0219533553 : MOBILE
so why we get different result between your web phone number parser and my simple project? did I do wrong implementation of your library? could you please take a look at my phoneValidation.js file?
from libphonenumber-js.
Yes, this turned out to be a small bug regarding FIXED_LINE_OR_MOBILE
cases.
Fixed in the new release.
from libphonenumber-js.
cool, it works for the latest version.
thank you for your support @halt-hammerzeit
👍
from libphonenumber-js.
Related Issues (20)
- Missing area code 861 HOT 1
- ts(1479) error in index.d.cts HOT 3
- Unable to delete characters if there is a parenthesis HOT 1
- Any property returns area code? HOT 3
- Different Results | Real Life vs Demo HOT 2
- validatePhoneNumberLength does not return TOO_LONG for invalid Ukrainian phone number HOT 9
- Some North American countries receive incorrect National Format when removing digits
- Cuba added a new prefix 63 to phone number HOT 1
- New HK Hutchison telecom mobile number range not yet updated HOT 2
- Spain accepts 7 as a prefix for phone numbers HOT 1
- Unable to get Number Type of some numbers like Indian Mobile Numbers HOT 4
- Is this Error expected? HOT 5
- Missing dashes when using International format HOT 3
- Failed running script liphonenumber-generate-metadata HOT 1
- Find phone numbers in text doesn't work if fr and us are mixed HOT 1
- Find and replace in text problem
- +13107549 is somehow valid? HOT 2
- Country codes are missing 'AQ' (Antarctica) code HOT 1
- AsYouType doesn't work for "FR" and other countries. Only works for "US".
- deleted because no general issue with libphonenumber-js HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from libphonenumber-js.