В следующем коде я передаю FormControl
, который содержит пароль. Я ожидаю, что когда пароль aA1[11]
, метод RegExp.test
должен возвращать false, но он возвращает true! Почему мой код возвращает null
вместо объекта ошибки {
validatePassword: {
valid: false,
message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'
}
Если этот форвард не проверит совпадение (?=.*[!@#$%^&*()_+}{":'?>.<,])
validatePassword(control: FormControl) {
let password: string = control.value;
/* So the rule for password is
6-10 length
contains a digit
contains a lower case alphabet
contains an upper case alphabet
contains one more special character from the list !@#$%^&*()_+}{":;'?/>.<,
does not contain space
*/
let REG_EXP = new RegExp('(?=^.{6,10}$)(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()_+}{":\'?>.<,])(?!.*\\s).*$');
/*RegExp's test method returns true if it finds a match, otherwise it returns false*/
console.log('password: ',password);
console.log('test result ',(REG_EXP.test(password)));
return (REG_EXP.test(password)) ? null : {
validatePassword: { //check the class ShowErrorsComponent to see how validatePassword is used.
valid: false,
message: 'password must contain 1 small-case letter [a-z], 1 capital letter [A-Z], 1 digit[0-9], 1 special character and the length should be between 6-10 characters'
}
}
}
Я вызываю вышеуказанную функцию из моего теста кармы
fit('A password of length between 6-10 characters and containing at least 1 digit, at least 1 lowercase letter, at least 1 upper case ' +
'letter and but NOT at least 1 special character from the list !@#$%^&*()_+}{":;\'?/>.<, shall NOT be accepted',
inject([HttpClient,HttpTestingController],(httpClient:HttpClient)=>{
let helper = new HelperService(loaderService,httpClient);
let passwordField = new FormControl();
let password = 'aA1[11]';
passwordField.setValue(password);
let result = helper.validatePassword(passwordField);
expect(result).toEqual(expectedErrorResponse);
}));
Вывод, который я вижу в консоли,
password: aA1[11]
test result true