0

I'm trying to have some restriction on a text input.

I want it to accept only :

  • alphanumerical characters (a-z A-Z 0-9)
  • 3 special characters (comma, dash, single quotation mark) : , - '
  • A few accented characters : à â ç è é ê î ô ù û
  • spaces

So I'm declaring a validator with a regex pattern to the input like this :

this.form.addControl('adresseLine2', new FormControl('', Validators.pattern(PatternValidatorEnum.ADRESS_CONTENT)));

And the ADRESS_CONTENT regex is as follows :

ADRESS_CONTENT = '^[A-Za-z0-9\',\-àâçèéêîôùû ]*$'

It seems to work fine, but, the input stays valid when I have uppercase accented characters like : É

I want it to accept only lowercase accented characters (àâçèéêîôùû)

And according to https://regex101.com/r/6vyA8H/1 : dsdséàê ké should be a valid input but dsdséàê kÉ should not.

However, both are considered valid on my input.

Also, my input accepts characters such as ., ;, / when it shouldn't according to the regex.

What am I missing ? Also, there might be some cases I forgot in my regex.

Ellone
  • 3,644
  • 12
  • 40
  • 72
  • 1
    `ADRESS_CONTENT = '[A-Za-z0-9\',àâçèéêîôùû -]*'` or `ADRESS_CONTENT = /^[A-Za-z0-9',\-àâçèéêîôùû ]*$/` will both work. – Wiktor Stribiżew Oct 21 '19 at 16:53

2 Answers2

1

The issue here is not your expression but instead you using Validators.Pattern() with a string instead of a RegExp type, changing your expression from

'^[A-Za-z0-9\',\-àâçèéêîôùû ]*$'

to

/^[A-Za-z0-9\',\-àâçèéêîôùû ]*$/

will fix the issue.

Tiago Silva
  • 2,299
  • 14
  • 18
0

How about using this regex pattern?

/^(?!À-Ö)[A-Za-z0-9\',\-àâçèéêîôùû ]*$/

You can say exclude the Upper Accent characters. This regex supports all your inputs.

https://regex101.com/r/6vyA8H/2

Stackblitz: https://stackblitz.com/edit/angular-8-reactive-form-8f5xmk

Suhas Parameshwara
  • 1,344
  • 7
  • 15