React-number-format: Allow optional format

Created on 4 Oct 2017  路  3Comments  路  Source: s-yadav/react-number-format

In the README, there is an example of credit card validation. The thing is that not all credit card are 16 characters length, american express cards have 15 characters, so, the current example will not work with those credit cards.

A solution would be to allow multiple formats or optional formats, like:

#### #### #### #### || #### #### #### ###

or

#### #### #### ###(#)

is this possible? or there is another solution with the current api?

Thanks!!!

Most helpful comment

Any possibility of having this being supported now?

Brazilian cellphones can have 10 or 11 numbers:

(##) # ####-####
or
(##) ####-####

UX would not be very good if I ask users which kind of phone number they have.

I thought of using the smaller pattern first and if the user tries to input more numbers, then switch to the larger one. However onChange for that doesn't seem to be an option, since it will not trigger after the max length of the input is reached.

All 3 comments

You can have a different format based on your state. Something like

<NumberFormat  format={this.state.cardType === 'ae' ? '#### #### #### ###' : '#### #### #### ####'}/> 

You can also add onChange handler and deside the card type based on first 4 digits, and then change your cardType state.

Any possibility of having this being supported now?

Brazilian cellphones can have 10 or 11 numbers:

(##) # ####-####
or
(##) ####-####

UX would not be very good if I ask users which kind of phone number they have.

I thought of using the smaller pattern first and if the user tries to input more numbers, then switch to the larger one. However onChange for that doesn't seem to be an option, since it will not trigger after the max length of the input is reached.

Any possibility of having this being supported now?

Brazilian cellphones can have 10 or 11 numbers:

(##) # ####-####
or
(##) ####-####

UX would not be very good if I ask users which kind of phone number they have.

I thought of using the smaller pattern first and if the user tries to input more numbers, then switch to the larger one. However onChange for that doesn't seem to be an option, since it will not trigger after the max length of the input is reached.

If anyone is looking for a Brazilian phone number format option with 10 or 11 numbers, this is how I did:

const [phoneFormat, setPhoneFormat] = useState<string>('(##) ####-#####')
  const handlePhoneFormat = (e: EventTarget & HTMLInputElement) => {
    const phoneLength = e.value.replace(/\s/g, '').length

    if (phoneLength > 13) {
      setPhoneFormat('(##) # ####-####')
    } else if (phoneLength === 13) {
      setPhoneFormat('(##) ####-#####')
    }
  }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

everlasting-neverness picture everlasting-neverness  路  5Comments

TkDodo picture TkDodo  路  5Comments

nourahassan picture nourahassan  路  5Comments

fedulovivan picture fedulovivan  路  6Comments

havantuan picture havantuan  路  7Comments