1

I've created a simple custom validation function.

import { AbstractControl, ValidatorFn } from "@angular/forms";

export function firstLeterUpperCase():ValidatorFn{
    return (control:AbstractControl) =>{
        const value = <string>control.value;
        if(!value) return;
        if(value.length === 0) return;
        const firstLetter = value[0];
        if(firstLetter  !== firstLetter.toUpperCase())
        {
            return{
                  firstLeterUpperCase:{
                   message:"First letter must be uppercase."
               }
            }
        }
        return
    }
}

but I've got the following error. Not sure where I'm doing wrong.

Error: src/app/validators/firstLetterValidator.ts:4:5 - error TS2322: Type '(control: AbstractControl) => {} | undefined' is not assignableenter code here to type 'ValidatorFn'. Type '{} | undefined' is not assignable to type 'ValidationErrors | null'. Type 'undefined' is not assignable to type 'ValidationErrors | null'.

1
  • From the error message, ValidatorFn type must return ValidationErrors or null, but you have returned undefined (return; is same as return undefined;). Maybe it should be return null; instead. Commented Jul 21, 2021 at 3:27

2 Answers 2

2

I guess you are using the newest version of Angular. You can try this code:

 import { AbstractControl, ValidationErrors, ValidatorFn } from "@angular/forms";  


 export function firstLetterUppercase(): ValidatorFn {
        return (control: AbstractControl) : ValidationErrors | null => {
            const value  = <string>control.value;
            if(!value) return null;
            if(value.length === 0) return null;
    
            const firstLetter = value[0];
            if(firstLetter !== firstLetter.toUpperCase()){
                return {
                    firstLetterUppercase : {
                        message: 'The first letter must be uppercase'
                    }
                }
            }
            return null;
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Can you add some explanation about what's the problem in the OP's code and how your code fixes it?
1

Try with a simple function that does what you need:

export function firstLetterUppercase(): ValidatorFn {
  return (control: AbstractControl): {[key: string]: any} | null => {
    return /^[A-Z]/.test(control.value) ? null : { firstLetterUpperCase: true };
  };
}

Where we use the regex /^[A-Z]/ to test the control.value and see if it has the first letter uppercase. The function will return null if there are no errors otherwise it returns { firstLetterUpperCase: true }. Then associate the validator to your input (read it) and show or hide the error message directly in the template.

<div class="error-message" *ngIf="yourInputName.errors.firstLetterUpperCase">
 First letter must be uppercase.
</div>

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.