最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Ionic Angular: uppercasing form field value does not work on last character - Stack Overflow

programmeradmin0浏览0评论

In order to uppercase the textual input in a form's field, I have following code:

<ion-item>
  <ion-input label="Registration number *" labelPlacement="stacked" formControlName="carRegNr" type="text" autocapitalize="characters" placeholder="Registration number" clearInput="true" oninput="this.value = this.value.toUpperCase()">
  </ion-input>
</ion-item>

Problem now is that the last character of the input does not get capitalized. I.e. when I have a reference to the field value, it is shown as e.g. "CAR-REGi"

<span *ngIf="taskForm.value.carRegNr" class="form-header-label-text">{{taskForm.value.carRegNr}}</span>

Also when sending the value to the back-end, "CAR-REGi" is saved.

Am I overlooking something? How can I make sure the full string is uppercased?

In order to uppercase the textual input in a form's field, I have following code:

<ion-item>
  <ion-input label="Registration number *" labelPlacement="stacked" formControlName="carRegNr" type="text" autocapitalize="characters" placeholder="Registration number" clearInput="true" oninput="this.value = this.value.toUpperCase()">
  </ion-input>
</ion-item>

Problem now is that the last character of the input does not get capitalized. I.e. when I have a reference to the field value, it is shown as e.g. "CAR-REGi"

<span *ngIf="taskForm.value.carRegNr" class="form-header-label-text">{{taskForm.value.carRegNr}}</span>

Also when sending the value to the back-end, "CAR-REGi" is saved.

Am I overlooking something? How can I make sure the full string is uppercased?

Share Improve this question asked Nov 20, 2024 at 11:15 NorthFredNorthFred 5003 gold badges8 silver badges26 bronze badges 1
  • do you need the database saved value to be uppercase? – Naren Murali Commented Nov 20, 2024 at 11:17
Add a comment  | 

1 Answer 1

Reset to default 1

You should ensure you use (input) instead of onInput, the former is the angular way of event binding, the latter is javascript way.

<ion-item>
  <ion-input label="Registration number *" labelPlacement="stacked" formControlName="carRegNr" type="text" autocapitalize="characters" placeholder="Registration number" clearInput="true" (input)="this.value = this.value.toUpperCase()">
  </ion-input>
</ion-item>

Also you should use formControlName to store the value of the latest data. Then you can rewrite your code to.

HTML:

<ion-item>
  <ion-input label="Registration number *" labelPlacement="stacked" formControlName="carRegNr" type="text" autocapitalize="characters" placeholder="Registration number" clearInput="true" (input)="onInput()">
  </ion-input>
</ion-item>

<span *ngIf="taskForm.value.carRegNr" class="form-header-label-text">{{taskForm.value.carRegNr | uppercase}}</span>

Now update on the TS to uppercase, if you do not want the uppercase in the database then go for uppercase pipe.

onInput() {
  const ctrl = this.form.get('carRegNr');
  ctrl.patchValue(ctrl.value ? ctrl.value.toUpperCase() : ctrl.value);
}
发布评论

评论列表(0)

  1. 暂无评论