I'm building an Angular2 client side application. I'm currently working on the membership components and integrating client side components with MVC6 vNext Identity v3. I have written custom Angular2 password validators as follows:
needsCapitalLetter(ctrl: Control): {[s: string]: boolean} {
if(!ctrl.value.match(/[A-Z]/))
return {'needsCapitalLetter': true}
return null;
}
needsLowerLetter(ctrl: Control): {[s: string]: boolean} {
if(!ctrl.value.match(/[a-z]/))
return {'needsLowerLetter': true}
return null;
}
needsNumber(ctrl: Control): {[s: string]: boolean} {
if(!ctrl.value.match(/d/))
return {'needsNumber': true}
return null;
}
needsSpecialCharacter(ctrl: Control): {[s: string]: boolean} {
if(!ctrl.value.match(/[^a-zA-Zd]/))
return {'needsSpecialCharacter': true}
return null;
}
This work great, and I'm loving Angular2, but now I'm trying to write a validator that verifies that the "Confirm Password" is equal to the "Password". In order to do this, I need to be able to validate one field against the other. I can easily do this at the component level, and just check on blur, or on submit, or any number of other ways, but this bypasses the Angular2 ngForm validation system. I would very much like to figure out how to write an Angular2 Validator for a field that can check the value of another field, by passing in the name of the other field or something close to this. It seems this should be a capability as this would be a necessity in almost any complex business application UI.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…