当它无效时,使用 asYouType 在 libphonenumber 中获取真正的有效号码



我在反应中使用libphonenumber来格式化和验证电话号码(在我的情况下是黎巴嫩号码)。

黎巴嫩电话号码的有效掩码是 +961 xx xxx xxx 这在他们的演示中给出的示例中实际上运行良好 这里

因此,对于黎巴嫩,国家/地区代码LB并且有效的模板xx xxx xxx

例如,当输入+961 71 123 123==>电话号码实际上是valid+961 71 123 12不是

就我在 React 中而言,输入数字一旦达到2就有效,就像在应该达到371 123 12一样

有效
import { AsYouType } from 'libphonenumber-js'
let asYouType = new AsYouType()
asYouType.defaultCountry = 'LB';
asYouType.reset();
asYouType.input('7112312')
// <<< PROBLEM HERE >>>
console.log('number is valid ',asYouType.getNumber().isValid()); 
//output: is valid when it should not be valid 71 123 12 (missing     one number)

asYouType.defaultCountry = 'LB';
asYouType.reset();
asYouType.input('711231') 
console.log('number is valid ',asYouType.getNumber().isValid()); 
// out : false (OK)
asYouType.defaultCountry = 'LB';
asYouType.reset();
asYouType.input('71123123') 
console.log('number is valid ',asYouType.getNumber().isValid()); 
// out: true (OK)

libphonenumber-js默认使用减少的元数据集来减小包大小。你所看到的可能是这种简化的结果。

如果要使用完整的元数据集,请在导入库时使用/max后缀。

import { AsYouType } from 'libphonenumber-js/max'

有关提供的元数据集的更多信息,请查看文档。

最新更新