Search code examples
angularangular-validation

Angular : How to exclude backslash and double quotes in my validator pattern


I want to ensure that input fields doesn't contain backslash or Double Quotes characters

enter image description here enter image description here
What is the pattern please ?

 reportForm = this.fb.group({
summary: [null, [Validators.required, Validators.minLength(3), Validators.pattern()]], 
],

Solution

  • By adding {3,} instead of the + you could also remove the minLength Validator, since it is then included into the pattern.

    var pattern = /^[^\\"]+$/;
    console.log(pattern.exec('abc')); // 'abc' -> OK
    console.log(pattern.exec('ab"c')); // null -> ERR
    console.log(pattern.exec('\\abc')); // null -> ER