I am trying to validate the input fields using ngControl's value in angular 2. i need to validate that the user enters the value in upper case always.
Now we need to convert the value entered by user to uppercase. But i am handling values from input fields using ngControl, not ngModel ( considering i could have used ngModelChange event to update value to uppercase.)
So what is the best and low cost way to convert the value used by ngControl.
To convert input field text to uppercase in angular we can use (ngModelChange) method. We will go through an example to understand it further. We will create a component called UppercaseInputComponent in our Angular project and then will bind <input> field to a variable called inputValue using (ngModel) .
The angular. uppercase() Function in AngularJS is used to convert the string into uppercase.
The uppercase filter converts a string to uppercase letters.
To convert input field text to uppercase in angular we can use (ngModelChange) method. We will go through an example to understand it further. We will create a component called UppercaseInputComponent in our Angular project and then will bind <input> field to a variable called inputValue using (ngModel).
And also, How to use UpperCasePipe in Angular HTML and typescript components for example. Uppercase for a string is to convert all letters into Uppercase. We can achieve this using either writing our Code to convert or existing Angular UpperCasePipe. Angular core provides pipes UpperCasePipe which transforms a string into an Upper case string.
Form contains multiple html input elements to take input from the user. button allows the user to submit the from to backend API an d it calls click binding event. In Angular, View is html template and controller is an typescript component. Reading input text is basic concept every Angular developer need to know.
I developed a solution in Angular 7 for uppercase and lowercase, based in some posts i've read. But i tested only for reactive forms. This solution resolve the problem of the last word and the position of the cursor. stackblitz.com/edit/angular-form-uppercase-lowercase
As @Eric Martinez suggested, you can create a local template variable, and bind the uppercase string to the value property on the input event:
<input type="text" #input (input)="input.value=$event.target.value.toUpperCase()" />
Alternatively, you can make this a directive:
@Directive({ selector: 'input[type=text]', host: { '(input)': 'ref.nativeElement.value=$event.target.value.toUpperCase()', } }) export class UpperCaseText { constructor(private ref: ElementRef) { } }
To use the directive, specify UpperCaseText
in your component's list of directives:
directives: [UpperCaseText]
Demo Plnkr
Here is my solution:
Using host listener to listen input event and then force it to uppercase.
import {Directive, EventEmitter, HostListener, Output} from '@angular/core'; @Directive({ selector: '[ngModel][uppercase]' }) export class UppercaseDirective { @Output() ngModelChange: EventEmitter<any> = new EventEmitter(); value: any; @HostListener('input', ['$event']) onInputChange($event) { this.value = $event.target.value.toUpperCase(); this.ngModelChange.emit(this.value); } }
With this directive, you can easily force input to uppercase like this:
<input type="text" class="form-control" placeholder="ID" formControlName="id" [(ngModel)]="form.value.id" uppercase/>
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