0

Hi I am new to Angular Reactive forms and have been working on trying to figure this out.

What I want to do is format the input data to uppercase:/

<input #stateInput matInput type="text" 
formControlName="state" 
[matAutocomplete]="autoState" maxlength="2"
(keyup)="stateInput.value.toUpperCase()" 
required>

I haven't been able to get this to work?

Any help is appreciated

Thanks

1
  • I like use <input style="text-transform:uppercase"> and in submit trasnform the variable. Another way you must take account if you type in middle of the input Commented Apr 8, 2019 at 19:24

2 Answers 2

2

To achieve expected result , use below option

Issue: After changing input value to uppercase, it has to be reassigned to show in Input field

<input
    type="text"
    [formControl]="state"
    #stateInput
    (keyup)="stateInput.value = stateInput.value.toUpperCase()"
  />

code sandbox for reference - https://codesandbox.io/s/6l9nk4k2zn

Sign up to request clarification or add additional context in comments.

6 Comments

the problem with your code is when you try to keyup in middle of the input
@Eliseo, that scenario is not possible as per SO question , as maxlength is only two
::glups:: I dont see this detail
@Eliseo, i just tried to keyup in the middle of input , it works as expected - codesandbox.io/s/0pwrknw97l .. This works in that scenario because I am changing complete input value to uppercase not based on location :)
when I type "abde", and I put the cursor between b and d and type c (to form "abcde", the cursor goes to the end of the input. Therefore, if you write in the html {{state.value}}, you can see that the value is not to upper. If you put maxlength="2" and type ab, go yo the first, remove the "a" and type C, the cursor goes to the end too
|
1

you can just simple use below code which will convert input to UpperCase instantly ,

oninput="this.value = this.value.toUpperCase()"

here is an example Example

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.