GithubHelp home page GithubHelp logo

Comments (32)

catamphetamine avatar catamphetamine commented on July 3, 2024

getNumberType() always returns phone number type with the full metadata for a county.
With the default metadata use parse() instead.

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

That works too.
It's the same as parse(value, 'ID')

from libphonenumber-js.

catamphetamine avatar catamphetamine commented on July 3, 2024

Actually, there's a dedicated function for validating phone numbers: isValidNumber(value, 'ID')

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

Provide the exact code

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

I already explained that getNumberType only works for full metadata which is not the default one

from libphonenumber-js.

catamphetamine avatar catamphetamine commented on July 3, 2024

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.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

First compile custom metadata for ID with the --extended flag.
Next use either getNumberType() or isValidNumber().
And that should be working.

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

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.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

(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.

andangrd avatar andangrd commented on July 3, 2024

have you updated the Readme or any documentation?

from libphonenumber-js.

catamphetamine avatar catamphetamine commented on July 3, 2024

Yes, I added a note on "tree-shaking" and the /custom export

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

If you make up a sample project with simple reproduction instructions I could try that

from libphonenumber-js.

catamphetamine avatar catamphetamine commented on July 3, 2024

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.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024
➜  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.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

numberType for 085867899444 : MOBILE

Which means it's working.

What exactly do you think isn't working.

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

So which exact phone numbers you think aren't working and what is the exact expected result

from libphonenumber-js.

catamphetamine avatar catamphetamine commented on July 3, 2024

The one you said about is undetermined:
https://libphonenumber.appspot.com/phonenumberparser?number=0219533553&country=ID
FIXED_LINE_OR_MOBILE

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

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.

catamphetamine avatar catamphetamine commented on July 3, 2024

Yes, this turned out to be a small bug regarding FIXED_LINE_OR_MOBILE cases.
Fixed in the new release.

from libphonenumber-js.

andangrd avatar andangrd commented on July 3, 2024

cool, it works for the latest version.
thank you for your support @halt-hammerzeit
👍

from libphonenumber-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.