Hey there, I'm new to Angular 4 and in need of help validating a form field. I want to allow only numbers, but the first digit should not be 0.
I've tried different solutions that I found, but none of them worked for me.
Could someone please assist me with this?
This is what I have in my app.component.ts file:
NumberOnly(event: any) {
const pattern = /^([1-9][0-9]*|0)$/;
let inputChar = String.fromCharCode(event.charCode);
if (!pattern.test(inputChar)) {
event.preventDefault();
}
}
The code above allows only numbers, however, it does not validate the rule against having the first digit as ZERO.