Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert input value to uppercase in angular 2 (value passing to ngControl)

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.

like image 829
ankitkamboj Avatar asked Mar 06 '16 11:03

ankitkamboj


People also ask

How to convert input to uppercase in angular?

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) .

How would you take a property called name and convert it to uppercase in angular?

The angular. uppercase() Function in AngularJS is used to convert the string into uppercase.

Which of the following filter is used to convert input to all uppercase in angular?

The uppercase filter converts a string to uppercase letters.

How to convert input field text to uppercase in angular?

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).

How to use uppercasepipe in angular?

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.

What is the use of input form in angular?

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.

Is there a solution for uppercase and lowercase in Angular 7?

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


2 Answers

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

like image 57
pixelbits Avatar answered Sep 20 '22 18:09

pixelbits


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/> 
like image 39
subaru710 Avatar answered Sep 19 '22 18:09

subaru710