Home  >  Q&A  >  body text

How to properly format numbers on <TextInput> on the fly in React Native?

<TextInput value={initialMoney} 
    onChangeText={(x) => {
      setInitialMoney(x.replace(/\B(?=(\d{3})+(?!\d))/g, '.'));
    }}
/>

The above code will not work properly.

instead of showing 214.124.124.124

It will show 2.1.4.1.2.4.1.2.4.123

No problem with regex, I tried regex outside of TextInput and it worked fine. Can anyone help me solve this problem?

P粉396248578P粉396248578404 days ago526

reply all(1)I'll reply

  • P粉724256860

    P粉7242568602023-09-12 00:18:06

    There may be some problems with your regular expression. Trying to debug it, I checked via Regex101 and it showed the error.

    Try reading this articleHow to format numbers to currency when using React Native Expo?

    Hope this helps.

    reply
    0
  • Cancelreply