Angular Can';在从另一个类扩展一个类时,不能以被动形式执行信用卡格式化
从另一个类扩展类以验证信用卡号的特定字段时出现问题 我想将我的卡片格式显示为0000-0000-0000-0000 我使用了来自互联网的正则表达式模式,但它不起作用 CardNumberControl类文件 父类文件-卡片类文件Angular Can';在从另一个类扩展一个类时,不能以被动形式执行信用卡格式化,angular,typescript,angular-reactive-forms,Angular,Typescript,Angular Reactive Forms,从另一个类扩展类以验证信用卡号的特定字段时出现问题 我想将我的卡片格式显示为0000-0000-0000-0000 我使用了来自互联网的正则表达式模式,但它不起作用 CardNumberControl类文件 父类文件-卡片类文件 我看到你在使用反应式表单 我使用了ngx掩码(每周下载 106417)在我的项目中处理类似的格式,这非常好 这是一个简单的配置和用法 在模块中: import { IConfig, NgxMaskModule } from "ngx-mask";
我看到你在使用反应式表单 我使用了ngx掩码(每周下载 106417)在我的项目中处理类似的格式,这非常好 这是一个简单的配置和用法 在模块中:
import { IConfig, NgxMaskModule } from "ngx-mask";
....
import { IConfig, NgxMaskModule } from "ngx-mask";
....
const NGX_LOADING_CONFIG = new NgxLoadingConfiguration();
export const NGX_MASK_OPTIONS: Partial<IConfig> = {
thousandSeparator: ",",
};
@NgModule({
declarations: [
...
],
imports: [
...
NgxMaskModule.forRoot(NGX_MASK_OPTIONS),
],
exports: [...],
providers: [
....
],
})
export class AdminModule {}
从“ngx掩码”导入{IConfig,NgxMaskModule};
....
从“ngx掩码”导入{IConfig,NgxMaskModule};
....
const NGX_load_CONFIG=new NgxLoadingConfiguration();
导出常量NGX_掩码_选项:部分={
千位分隔符:“,”,
};
@NGD模块({
声明:[
...
],
进口:[
...
NgxMaskModule.forRoot(NGX_掩码_选项),
],
出口:[……],
供应商:[
....
],
})
导出类AdminModule{}
在组件中,只需
<label for="code">Code</label>
<input
id="code"
type="text"
class="form-control"
formControlName="code"
placeholder="00-00-00"
mask="00-00-00"
/>
code
当然,在您的情况下,掩码是0000-0000-0000-0000
也许我丢失了一些代码,因为我不久前确实使用过它,但是包的文档已经非常清晰明了。我看到您使用的是反应式表单 我使用了ngx掩码(每周下载 106417)在我的项目中处理类似的格式,这非常好 这是一个简单的配置和用法 在模块中:
import { IConfig, NgxMaskModule } from "ngx-mask";
....
import { IConfig, NgxMaskModule } from "ngx-mask";
....
const NGX_LOADING_CONFIG = new NgxLoadingConfiguration();
export const NGX_MASK_OPTIONS: Partial<IConfig> = {
thousandSeparator: ",",
};
@NgModule({
declarations: [
...
],
imports: [
...
NgxMaskModule.forRoot(NGX_MASK_OPTIONS),
],
exports: [...],
providers: [
....
],
})
export class AdminModule {}
从“ngx掩码”导入{IConfig,NgxMaskModule};
....
从“ngx掩码”导入{IConfig,NgxMaskModule};
....
const NGX_load_CONFIG=new NgxLoadingConfiguration();
导出常量NGX_掩码_选项:部分={
千位分隔符:“,”,
};
@NGD模块({
声明:[
...
],
进口:[
...
NgxMaskModule.forRoot(NGX_掩码_选项),
],
出口:[……],
供应商:[
....
],
})
导出类AdminModule{}
在组件中,只需
<label for="code">Code</label>
<input
id="code"
type="text"
class="form-control"
formControlName="code"
placeholder="00-00-00"
mask="00-00-00"
/>
code
当然,在您的情况下,掩码是0000-0000-0000-0000
也许我丢失了一些代码,因为我不久前确实使用过它,但是包的文档已经非常清晰明了了。我建议您使用它,它提供了一个内置的验证来对信用卡号字段执行验证 在下面的示例中,
creditCardNumber
字段根据从creditCardTypes
数组中选择的cardType进行验证,并根据所选值进行相应验证
以下是组件代码:
导出类UserComponent实现OnInit{
userFormGroup:FormGroup;
信用卡类型=[
“签证”,
“AmericanExpress”,
“大师”,
“JCB”,
“发现”,
“餐厅俱乐部”,
“万事达卡”
];
构造函数(私有formBuilder:formBuilder){}
恩戈尼尼特(){
this.userFormGroup=this.formBuilder.group({
卡片类型:[“”],
信用卡号码:[
"",
RxwebValidators.creditCard({fieldName:“cardType”})
]
});
}
}
组件html代码
卡片类型
{{item}}
根据卡类型的选择,应用条件验证
{{userFormGroup.controls.cardType.errors.creditCard.message}}
信用卡号。
输入所选卡的卡号
{{userFormGroup.controls.creditCardNumber.errors.creditCard.message}}
提交
下面是一个完整的示例我建议您使用它提供了一个内置的验证来对信用卡号字段执行验证 在下面的示例中,
creditCardNumber
字段根据从creditCardTypes
数组中选择的cardType进行验证,并根据所选值进行相应验证
以下是组件代码:
导出类UserComponent实现OnInit{
userFormGroup:FormGroup;
信用卡类型=[
“签证”,
“AmericanExpress”,
“大师”,
“JCB”,
“发现”,
“餐厅俱乐部”,
“万事达卡”
];
构造函数(私有formBuilder:formBuilder){}
恩戈尼尼特(){
this.userFormGroup=this.formBuilder.group({
卡片类型:[“”],
信用卡号码:[
"",
RxwebValidators.creditCard({fieldName:“cardType”})
]
});
}
}
组件html代码
卡片类型
{{item}}
根据卡类型的选择,应用条件验证
{{userFormGroup.controls.cardType.errors.creditCard.message}}
信用卡号。
输入所选卡的卡号
{{userFormGroup.controls.creditCardNumber.errors.creditCard.message}}
提交
下面是完整的示例