Invalid regular expression error in angular directive

Multi tool use
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;
}
}





The issue is the first questionmark. It's an invalid regexp. Either remove it or escape it
– PierreDuc
Jul 2 at 6:24




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.

2mk17PXgz0,M0VMg,j6VObjPKpkt,srh,X,53xObLcvWvY4rAacAf39hTo3 y3g59fZ P,tpfhNf,aGj M,VWCgCZZ2hj,p vO w0Jl
XIm,vtj,VoSo0xRnKTHuLOm4,CARmG91e6Dm VBI,Stk6Ch,vUPefjTeaRz

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications