Reputation: 11
My type script code to restrict only alphabets:
InputValidatorChar(event:any){
Const pattern= /^[a-zA-Z]*$/;
If(! pattern.test(event.target.value)){
event.target.value=event.target.value.replace(/[^a-zA-Z/g,"");
}}
Expected output: Should accept only alphabets (example: jekrhrjek)
Output I am getting: Accepting only characters. But if I type any integer at end of the sentence as input and click outside of cell then the last typed integer is getting populated.
How to overcome this problem?
Upvotes: 1
Views: 289
Reputation: 163632
Instead of first testing if there are only chars A-Z a-z or an empty string, you might opt for running only the replacement.
Note that you have to close the character class [^a-zA-Z]+
and you can match 1 or more times to replace it with an empty string.
InputValidatorChar(event:any){
event.target.value=event.target.value.replace(/[^a-zA-Z]+/g, "");
}
Upvotes: 1