ghost2092000
ghost2092000

Reputation: 377

How to fix ' Argument of type 'number' is not assignable to parameter of type 'string | RegExp' '

I am trying to prevent the user from using any special symbols and also from having blank spaces without a character. When I try to put it on my FormGroup Validator I get an error saying 'Argument of type 'number' is not assignable to parameter of type 'string | RegExp''

this.houseForm = this.fb.group({
      address: [
        null,         
        [Validators.required, Validators.pattern(^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*)],
      ]
    });

Upvotes: 0

Views: 1525

Answers (1)

ChrisG
ChrisG

Reputation: 2948

Validators.pattern() is expecting either a string or a regex. You are providing neither.

You have provided ^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*, but what you need is either:

/^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*/ or

'^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*'

Need to surround it with slash / for Regex or quote ' for string.

https://angular.io/api/forms/Validators#pattern

Upvotes: 2

Related Questions