0
votes

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 ]*)],
      ]
    });
1
Validators.pattern(/^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*$/)?Wiktor Stribiżew
That's the regex part, but I get an error saying 'Argument of type 'number' is not assignable to parameter of type 'string | RegExp''ghost2092000
Try passing as a string - stackoverflow.com/questions/42392373/…Vandesh
@Vandesh It is the same. Validators.pattern accepts both. Validators.pattern(/^[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*$/) = Validators.pattern("[A-Za-z0-9 ]*[A-Za-z0-9][A-Za-z0-9 ]*")Wiktor Stribiżew
To clarify, the detail being pointed out is that your pattern is not enclosed within slashes (i.e. /, which is the syntax for instantiating a RegExp object), nor is it enclosed within quotes (which would make it a string literal).chuckx

1 Answers

2
votes

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