Skip to content
Advertisement

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.

Advertisement

Answer

This could work.

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

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement