Search code examples
reduxpaddingnormalizationredux-form

Normalizer for padding a user's input


i have an input field who accepts only numeric values. This field must have a normalizer that autofill a user input with zeros. Also the length must not be greater than eight. For example if i type 1 the field must be prefilled with 00000001. If i type 00000012. Do you know how to achieve such an action?


Solution

  • You can define your normalize function like:

    const normalizer = (value) => String("00000000" + value).slice(-8);
    

    this will convert input into a 8 digits length, 0-padded value.

    Then you simply define your Field as

    <Field name="number" component="input" type="number" normalize={normalizer} />
    

    See working example here