35

I'm working with angular (typescript) and I have a modelform in html where the user has to insert a code field and a description field.

The code field must always be entered by the user, always in uppercase.

I found and followed this question: How to convert input value to uppercase in angular 2 (value passing to ngControl)

but the last letter that the user inserts remains lowercase however .. The fundamental thing is that the database always comes in all uppercase (I also put a limit of 4 characters that works properly) this is my code now, but as written above it does not work properly:

<input type="text" id="code" #code class="form-control" formControlName="code" maxlength="4"
                 (input)="code.value=$event.target.value.toUpperCase()">

has anyone found a quick, functional and fast solution?

thank you!

spierala
  • 2,349
  • 3
  • 25
  • 50
Nobady
  • 1,074
  • 2
  • 11
  • 35

7 Answers7

106

You can simply add oninput="this.value = this.value.toUpperCase()" in your <input> tag and it will instantly convert any input in your input field to Uppercase.

Ali Heikal
  • 3,790
  • 3
  • 18
  • 24
  • 2
    Yes, i had just found this solution and I tested it, it works properly! thank you all! – Nobady May 21 '18 at 10:49
  • 3
    (input)="code.value=$event.target.value.toUpperCase()" doesn't work properly. It capitalizes all letters but the last. @Ali thanks for your solution. Works properly. – D_Edet Jul 31 '20 at 08:53
  • 13
    Attention! This solution has a side effect. When you want to change the input in the middle or at the start then the cursor will jump to the end of the line while typing – WorstCoderEver May 24 '22 at 12:30
26

If you're working with ngModel, you can use ngModelChange to do it this way with JavaScript's .ToUpperCase().

<input [ngModel]="person.Name" (ngModelChange)="person.Name = $event.toUpperCase()">
DForsyth
  • 498
  • 8
  • 19
11

Based on @Ali Heikal answer it should be:

<input #code formControlName="code" (input)="code.value = code.value.toUpperCase()" type="text">

Or use .toLocaleUpperCase() if necessary.

hovado
  • 4,474
  • 1
  • 24
  • 30
  • 2
    As pointed out by @D_Edet on another answer, this won't capitalize the last letter directly on the model, visually it shows all capitalized but if you check the associated form value it won't. keyup event with form.patchValue works thanks to Sampaguita. – le0diaz Sep 30 '21 at 03:45
10

You could create an Angular Directive which transforms all user input to uppercase.

The Directive extends DefaultValueAccessor:

@Directive({
  selector: 'input[toUppercase]',
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      multi: true,
      useExisting: forwardRef(() => UpperCaseInputDirective),
    },
  ],
})
export class UpperCaseInputDirective extends DefaultValueAccessor {
  @HostListener('input', ['$event']) input($event: InputEvent) {
    const target = $event.target as HTMLInputElement;
    const start = target.selectionStart;

    target.value = target.value.toUpperCase();
    target.setSelectionRange(start, start);

    this.onChange(target.value);
  }

  constructor(renderer: Renderer2, elementRef: ElementRef) {
    super(renderer, elementRef, false);
  }
}

Use the Directive like this on the input:

<input name="myTdfInput" type="text" [(ngModel)]="value" toUppercase>

The solution works in both Reactive Forms and Template Driven Forms.

See working demo on Stackblitz: https://stackblitz.com/edit/angular-input-field-to-accept-only-numbers-czivy3?file=src/app/to-uppercase.directive.ts

Credits for the solution go to: https://twitter.com/BartBurgov (See his tweet here: https://twitter.com/BartBurgov/status/1582394428748009477?s=20&t=RiefRcZ1B8IF42wp2rWzaQ)

spierala
  • 2,349
  • 3
  • 25
  • 50
  • 1
    Great approach to consistently transform input to uppercase and also ensures that the cursor position is correctly set aftwards. Especially useful for reactive forms. – anme98 Apr 06 '23 at 10:02
8

A bit late, but I'm surprised to don't see the css alternative.

Add on the input field:

.input-upper{
    text-transform: uppercase;
}
movaction.com
  • 537
  • 6
  • 6
  • 15
    Note that this is purely cosmetic for display purposes, it doesn't change the underlying value. If you need it uppercase for storage, you may need to use one of the other solutions. – andreialecu Jan 13 '21 at 13:07
  • 3
    Agreed -- this doesn't deserve a downvote, but a very strong disclaimer. Sometimes, this would be perfectly fine. – Chris Baker Feb 10 '21 at 03:55
  • I wanted to display a disabled input as uppercase. This solution works perfectly in my case! – Boris BELLOC Jan 31 '23 at 13:18
6

This worked for me. I just used keyup and whenever a user adds a character it automatically converts it to Uppercase, not just in the UI but in the model as well.

(keyup)="form.patchValue({name: $event.target.value.toUpperCase()})"
Sampaguita
  • 96
  • 1
  • 4
4

You can use a pattern attribute to limit the allowed input. You may also want to assist the user by upper-casing their input.

<input type="text" pattern="[A-Z]*" name="example" />
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 3
    Base on my tests with this snippet, pattern doesn't limit the input. In FF it just flags an error by turning the input box red. In Chrome, it appears to have no affect. – JeffryHouser Sep 06 '18 at 12:57
  • what about making first letter capital what the pattern – Ilia Tapia May 11 '19 at 10:08