Han Che
Han Che

Reputation: 8519

angular 2 access custom validator return to inform user

here's a simple custom validator in angular 2

hasUpperCase(control:FormControl):{[s:string]:boolean}{
    if (/[A-Z]/.test(control.value) === true { return null }
    else{ return {noUpperCase:true} }
}

How can I access the else return, so that I can prompt the user that the input has no upper case?

Thanks!

Upvotes: 0

Views: 37

Answers (1)

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657666

Get a reference to the form (I can't see from your code how you use forms or what version) (or get a reference to the control directly), look up the control the validator is applied to read it's errors property. It contains all the error objects returned by all failed validators.

Upvotes: 1

Related Questions