我正在尝试使用Reactive表单在Angular中创建用户注册表单。 我希望密码验证,如它应该以大写字母开头,至少有一个数字,至少一个特殊字符,并且对小写字母没有限制。
我拥有的是:
ngOnInit(): void {
this.customerForm = this.fb.group({ //root group [formGroup]
firstName: ['', [Validators.required, Validators.minLength(3)]],
lastName: ['', [Validators.required, Validators.minLength(2), Validators.maxLength(50)]],
password: ['',[Validators.required, Validators.minLength(6), Validators.maxLength(25), Validators.pattern('')]],
// Validators.pattern('')
这里我需要使用回归表达式,但我不知道哪个是适合我的要求
emailGroup: this.fb.group({
email: [null, [Validators.required, Validators.pattern("[a-z0-9._%+-]+@[a-z0-9.-]+")]],
confirmEmail: ['', Validators.required],
}, { validator: emailMatcher }),
country: [null, [Validators.required]],
})
let getNotification = this.customerForm.get('notification')
getNotification.valueChanges
.subscribe(value => this.sentNotification(value)) //subscribe is like a for each so it displays the output
const emailControl = this.customerForm.get('emailGroup.email');
emailControl.valueChanges
.debounceTime(1000)
.subscribe(value =>
this.setMessageForEmail(emailControl));
const firstNameControl = this.customerForm.get('firstName')
firstNameControl.valueChanges
.subscribe(value =>
this.setMessageForFirstName(firstNameControl))
}
答案 0 :(得分:1)
您正在寻找的模式是
[A-Z](?=.*\d)(?=.*[---ALL YOUR SPECIAL CHARS HERE])(?=.*[a-z])
或接近于此。它使用positive lookahead来查找您的特殊字符和您的号码