验证输入长度
Validate input length
我正在使用 Angular
和 Angular Material
。使用 minlength
验证器验证输入长度时,空输入会漏过。是否有内置支持来验证 "empty or shorter than"?
我可以将 required
与 minlength
结合使用,但是,Angular Material
为此类输入设置样式,这是不可取的。
我应该实施自定义验证器吗?
I could use required in combination with minlength, however, Angular
Material styles such input and that's not desirable.
您可以简单地覆盖它们的样式,这不是问题。
或者您可以像您所说的那样创建一个自定义验证器。但这应该与 FormBuilder
一起使用,我不确定您正在使用它。
这里有一个你可以使用的,当然要给一个更好的名字:
export const MyCustomValidator = (length: number): ValidatorFn => {
return (control: AbstractControl): {[key: string]: any} => {
return ((!value ||) (value < length)) ? null : {
myCustom: true
};
};
};
我正在使用 Angular
和 Angular Material
。使用 minlength
验证器验证输入长度时,空输入会漏过。是否有内置支持来验证 "empty or shorter than"?
我可以将 required
与 minlength
结合使用,但是,Angular Material
为此类输入设置样式,这是不可取的。
我应该实施自定义验证器吗?
I could use required in combination with minlength, however, Angular Material styles such input and that's not desirable.
您可以简单地覆盖它们的样式,这不是问题。
或者您可以像您所说的那样创建一个自定义验证器。但这应该与 FormBuilder
一起使用,我不确定您正在使用它。
这里有一个你可以使用的,当然要给一个更好的名字:
export const MyCustomValidator = (length: number): ValidatorFn => {
return (control: AbstractControl): {[key: string]: any} => {
return ((!value ||) (value < length)) ? null : {
myCustom: true
};
};
};