Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react-number-format Showing Format On inserting value

react-number-format Showing Format On inserting value

I have used this package for the Phone Input format

import { PatternFormat } from 'react-number-format';

<PatternFormat 
   value={value} 
   className='form-control'   
   format="(###) ###-####" 
 />

Due to this format whenever I add any single value, The formatted value show in the input. before the value reaches there.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

I want to show the value in This format but when the value reaches there,
when I have entered only a single value it should not show that ‘-‘ there.

I want something like below
enter image description here

This is the link to the npm package I am using:
https://s-yadav.github.io/react-number-format/docs/intro

>Solution :

You could build the pattern diffrent if the value reaches said length then change (empty space) for a - (dash)

import { PatternFormat } from 'react-number-format';

let pattern;
if (value.length >= 9) {
    pattern = "(###) ###-####";
} else {
    pattern = "(###) ### ####";
}

<PatternFormat 
   value={value} 
   className='form-control'   
   format={pattern} 
 />

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading