我正在一个项目中,该项目必须允许数字(0-9),字符(a -z和A-Z)的组合,并且最小长度应为8。请帮助我。我尝试了很多,但是找不到一个好的解决方案。
ngOnInit() {
this.newPasswordFormGroup = this.formBuilder.group({
newPassword: ['', Validators.compose([Validators.required,
Validators.minLength(8),
Validators.pattern('^A-Za-z0-9')])],
confirmPassword: ['', Validators.required],
}, {
validator: MustMatch('newPassword', 'confirmPassword')
});
}
<form [formGroup]="newPasswordFormGroup" role="form" (ngSubmit)="onSubmitPassword()">
<div class="form-group text-left">
<input type="password" [ngClass]="{ 'is-invalid': submitted && f.newPassword.errors }" formControlName="newPassword" class="form-control input-underline input-lg textBox" id="newPassword" placeholder="Enter new password" />
<div *ngIf="submitted && f.newPassword.errors" class="invalid-feedback">
<div *ngIf="f.newPassword.errors.required">New password is required</div>
</div>
<div *ngIf="submitted && f.newPassword.errors" class="invalid-feedback">
<div *ngIf="f.newPassword.errors.pattern">#Password should be a minimum of 8 characters and contain a combination of letters and numbers</div>
</div>
<div *ngIf="submitted && f.newPassword.errors" class="invalid-feedback">
<div *ngIf="f.newPassword.errors.minlength">Password should be a minimum of 8 characters</div>
</div>
</div>
<div class="form-group text-left">
<input type="password" [ngClass]="{ 'is-invalid': submitted && f.confirmPassword.errors }" formControlName="confirmPassword" class="form-control input-underline input-lg textBox" id="confirmPassword" placeholder="Enter confirm password" />
<div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
<div *ngIf="f.confirmPassword.errors.required">Confirm password is required</div>
</div>
<div *ngIf="submitted && f.confirmPassword.errors" class="invalid-feedback">
<div *ngIf="f.confirmPassword.errors.mustMatch">Passwords must match</div>
</div>
</div>
<div class="password-btn">
<button type="submit" [disabled]="isDisabled" class="btn btn-primary submit-btn"> Submit </button>
</div>
</form>
例如: 它应该是“ s12sre34”
答案 0 :(得分:0)
尝试更新验证器Validators.pattern('(?=(?:\d*[A-Za-z]){1,})(?=(?:[A-Za-z]*\d){1,})[A-Za-z\d]{8}')])
说明:
(?=(?:\d*[A-Z]){1,})(?=(?:[A-Z]*\d){1,})[A-Za-z\d]{8}
(?=(?:\d*[A-Za-z]){1,})
对与[数字-字母-字母]模式匹配1次或多次的非捕获组的正向查找(?=(?:[A-Za-z]*\d){1,})
与第一个或多次出现的[字母-数字位数]模式匹配的非捕获组的正向预测[A-Za-z\d]{8}
匹配八位数字或字母,因此长度正确A-Za-z
可以只是A-Z
\d
与其他示例中的0-9
相同答案 1 :(得分:0)
我确实喜欢这样。它对我有用。
ngOnInit() {
this.newPasswordFormGroup = this.formBuilder.group({
newPassword: ['', Validators.compose([Validators.required,
Validators.minLength(8),
CustomValidator.findCombinationLettersAndNumbers()])],
confirmPassword: ['', Validators.required],
}, {
validator: MustMatch('newPassword', 'confirmPassword')
});
}
import { AbstractControl, ValidatorFn } from '@angular/forms';
export class CustomValidator {
public static findCombinationLettersAndNumbers(): ValidatorFn {
return (c: AbstractControl): { [key: string]: boolean } | null => {
// console.log('val:', c.value);
let isDigit = false;
let isCapsOrSmallLetter = false;
// let isSmallLetter = false;
let isSpecialChar = false;
if ((!/\d/.test(c.value))) {
// console.log('password must contain digits');
isDigit = false;
} else {
isDigit = true;
}
if (!/[A-Za-z]/.test(c.value)) {
// console.log('password must contain uppercase letter');
isCapsOrSmallLetter = false;
} else {
isCapsOrSmallLetter = true;
}
// if (!/[a-z]/.test(c.value)) {
// console.log('password must contain lowercase letter');
// isSmallLetter = false;
// } else {
// isSmallLetter = true;
// }
if (!/[ !@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(c.value)) {
// console.log('password must contain special character');
isSpecialChar = true;
}
if (isDigit && isCapsOrSmallLetter && isSpecialChar === true) {
// null is required here. otherwise form wonot submit.
return null;
}
return { passwordval: true };
};
}
}