0

This is my validator function. I need to pass the array of barcodes from my component. I have tried so many ways but nothing works.

 checkBarcodes(control: AbstractControl): { [key: string]: boolean } | null {
        let barcodes = ["6456", "6545", "2", "2", "121", "22", "11111", "22222"]
        for (let code of barcodes) {
            if (code == control.value) {
                return {
                    isValid: true
                }
            }
        }
        return null
    }

1 Answer 1

1
export function checkBarcodes(barcodes): ValidatorFn  {

    return (control : AbstractControl) : ({ [key: string]: boolean } | null) => {
        for (let code of barcodes) {
            if (code === control.value) {
                return  null ;
            }
        }
        return {'codeInvalid': true} ;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.