Angular - need regex for input javascript

Viewed 55

I need to write regex to validate the input. The validation which I need to achieve is if user type characters it should only accept 3 characters max and if user type digits it should accept 4 digits max and it should not accept any special characters in input field. currently I'm using this regex.

/^(\d{0,3}|[a-zA-Z]{0,2}|!([ ^*|\":<>[\]{}`\\()';@&$])+)$/gm

Actual result: when I type first time it's allowing me to type special character but when I'm type second time it's not allowing me to type.

Expected result: it shouldn't allow me to type any special character any time.

typescript code
let regex = /^(\d{0,3}|[a-zA-Z]{0,2}|!([ ^*|\":<>[\]{}`\\()';@&$])+)$/gm;
public validateFlightRules(e) {
return ((this.validateFlight && !!(searchText.match(regex))) || e.key === "Backspace");
}
<input appAutofocus #searchText type="text" (keydown)="validateFlightConnectionRules($event)"/>

note: I'm validating the input on keydown event. I can't use keyup event. Any help would be appreciated, thanks!

0 Answers
Related