【问题标题】:How to Validate mobile number with country code如何使用国家代码验证手机号码
【发布时间】:2021-11-23 15:22:59
【问题描述】:

我有两个输入,一个带有国家代码选项,例如 CA、US 等,另一个带有手机号码。我在一些源代码中看到使用 google-libphonenumber 进行验证,我在 Use google-libphonenumber in angular reactive forms validator 中找到了一个自定义验证,使用这个自定义验证器会给我带来一些错误返回 validNumber 吗? null : { 'wrongNumber': { value: control.value } };

输入 '{ wrongNumber: { value: any; }; } | null' 不能分配给类型 '{ [key: string]: any; }'。 类型 'null' 不能分配给类型 '{ [key: string]: any; }'。

如何传递国家代码和手机号码进行验证,如果无效则在 html 传递中显示错误。

【问题讨论】:

    标签: angular libphonenumber


    【解决方案1】:

    在您的函数 PhoneNumberValidator 中,尝试更改:

    return (control: AbstractControl): { [key: string]: any } => {
    

    return (control: AbstractControl): { [key: string]: any } | null => {
    

    如果它不起作用,请尝试另一种更改,单独(或混合):

    export function PhoneNumberValidator(regionCode?: string): (ValidatorFn | null | undefined)): ValidatorFn {
    

    【讨论】:

    • 如何在phonevalidator(regioncode)中传递一个formcontrol名称
    猜你喜欢
    • 2016-10-12
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多