角度2的验证器模式

时间:2016-10-11 17:35:20

标签: angular multidimensional-array angularjs-directive ionic-framework angular2-forms

如何在下面的代码中将正确的验证字符串附加到此 Validators.pattern数组

   password: ['', 
    Validators.compose([
    Validators.minLength(8), 
    Validators.maxLength(20),
    Validators.pattern('/(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])/') 
    ])],

1 个答案:

答案 0 :(得分:0)

你应该使用第一个FormControl来使用验证器,所以请遵循这个代码,不要忘记你应该将模式作为字符串传递,不用/:

import { FormControl, Validators } from '@angular/forms';
password: new FormControl(null, [
                Validators.required,
                Validators.minLength(8), 
                Validators.maxLength(20),
                Validators.pattern("(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z])")
            ]),