Search code examples
javascriptreact-nativetextinput

How to add in 4 alphanumeric with space in Text Input?


I would like to add a TextInput in this format:-

XXXX XXXX XXXX XXXX

for XXXX can be alphabet or numeric.

I getting this code but it just apply for numeric only.

_handlingCardNumber(number) {
  this.setState({
    cardNumber: number.replace(/\s?/g, '').replace(/(\d{4})/g, '$1 ').trim()
  });
}

How can I do it for numeric & string?

Please help.


Solution

  • This could work.

    number.replace(/\s?/g, '').replace(/(\d{4}|[a-zA-z]{4})/g, '$1 ').trim()