Invalid regular expression error in angular directive

Multi tool use
Invalid regular expression error in angular directive
Condition:
Contents can only contain characters from the following set:
a b c d e f g h i j k l m n o p q r s t u v w x y z
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
0 1 2 3 4 5 6 7 8 9
/ - ? : ( ) . , ' +
• Contents may NOT begin with ‘/’
• Contents may NOT contain ‘//’
export function directDebitValidator(nameRe: RegExp): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } | null => {
const directDebitID = nameRe.test(control.value);
return directDebitID ? { 'directDebit': { value: control.value } } : null;
};
}
@Directive({
selector: '[directDebit]',
providers: [{ provide: NG_VALIDATORS, useExisting: DirectDebitValidatorDirective, multi: true }]
})
export class DirectDebitValidatorDirective {
validate(control: AbstractControl): { [key: string]: any } | null {
return control.value ? directDebitValidator(new RegExp("^(? !.* [/]{2})[a-zA-Z0-9-?:().,'+]+([a-zA-Z0-9/-?:().,'+])*$"))(control)
: null;
}
}
1 Answer
1
There are a couple of issues:
[/-?]
-
/
So, you may use
directDebitValidator(new RegExp("^(?!.*/{2})[a-zA-Z0-9?:().,'+-][a-zA-Z0-9/?:().,'+-]*$"))
Or, using a regex literal notation:
directDebitValidator(/^(?!.*/{2})[a-zA-Z0-9?:().,'+-][a-zA-Z0-9/?:().,'+-]*$/)
See the regex demo.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
The issue is the first questionmark. It's an invalid regexp. Either remove it or escape it
– PierreDuc
Jul 2 at 6:24