DEV Community

HarmonyOS
HarmonyOS

Posted on

How to make the soft keyboard pop up to enter numbers?

Read the original article:How to make the soft keyboard pop up to enter numbers?

Question

How to make the soft keyboard pop up to enter numbers?

Short Answer

You can configure a TextInput component to display the numeric soft keyboard by setting its type to InputType.NUMBER_DECIMAL. Additionally, you can apply input filters and validation logic to control the format of entered numbers. Here you can find the related example below;

TextInput({ placeholder: 'Please enter content', text: this.message }) .width('100%') .type(InputType.NUMBER_DECIMAL) // Sets the input type to number/decimal //.maxLength(11) // Optional: restricts maximum length .inputFilter('^\d*\.?\d{0,3}$', (e) => { console.log('Regular expression - 3', JSON.stringify(e)) }) .backgroundColor(Color.Green) .onChange((value: string) => { this.message = value let charStr = value.charAt(0) if (charStr === '.') { this.message = value.replace('.', '0.') } })

Applicable Scenarios

  • When you need users to input only numbers or decimal values.
  • For fields like prices, amounts, quantities, or measurements that should restrict invalid characters.
  • When you want to enforce a specific format (e.g., maximum decimal places).

Reference Link

https://developer.huawei.com/consumer/en/doc/harmonyos-references-V5/ts-basic-components-textinput-V5#type

Written by Mehmet Algul

Top comments (0)