I am very new to Angular2
and cant seem to find my answer anywhere. I have an input
(as show below) but I only want it to allow the following:
I have no idea on how to do this. I have tried ng-pattern="/^[a-zA-Z\s]*$/"
, pattern="/^[a-zA-Z\s]*$/"
and ng-pattern-restrict="/^[a-zA-Z\s]*$/"
.
HTML
<td>
<md-input-container>
<input mdInput [(ngModel)]="details.firstName" placeholder="First name(s)" ng-pattern="/^[a-zA-Z\s]*$/">
</md-input-container>
</td>
Ideally if a user enters a numeric character, I'd ether like it to be removed by itself or just not be allowed (not displayed) in the field
To get a string contains only letters (both uppercase or lowercase) we use a regular expression (/^[A-Za-z]+$/) which allows only letters. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.
Allow Only Alphanumeric in Angular Input For handling the only alphanumeric in the input box we simply add an input control with the (keypress) event handler to call the keyPressAlphanumericl() .
Angular Reactive Form Validation - To accept only alphabets and space.
firstName: [
'',
[
Validators.required,
Validators.maxLength(50),
Validators.pattern('^[a-zA-Z ]*$')
]
],
My fix was to do it in my component
firstName: ['', Validators.pattern('^[a-zA-Z \-\']+')],
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With