Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom validator pattern with special characters Angular 4

I have a formgroup using custom validators:

return this.register = this.fb.group({
        username: ['', Validators.required, /* this.validateUsername()*/],
        email: ['', Validators.email, /*  this.validateUsername()*/],
        password: ['',
            [
                Validators.required,
                Validators.maxLength(50),
                Validators.minLength(8),
                Validators.pattern('^[a-zA-Z0-9!@#$%^&*()]+$'),
            ]
        ]
    });

I am trying to achieve validator pattern, but it doesn't work, someone help please.

like image 832
chris_r Avatar asked Aug 08 '26 18:08

chris_r


1 Answers

Make sure to add a / at start and the end of the regex string like this.

Validators.pattern(/^[a-zA-Z0-9!@#$%^&*()]+$/)
like image 142
Ulrich Dohou Avatar answered Aug 11 '26 07:08

Ulrich Dohou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!