最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I format AntD form input fields such as phone numbers and SSNs in React? - Stack Overflow

programmeradmin0浏览0评论

I have an AntD form with an Input field for phone numbers, and another to capture SSN's. As a user types a phone number or SSN, I would like the fields to format accordingly.

For example, instead of the phone number being 1234567890 as typed into the field, it should format as (123) 456-7890 with it initially being (___) ___-____. Similarly, in the SSN field if a user types their SSN such as 123456789 it should format as 123-45-6789 with it initially being ###-##-####.

What I'm really looking for is the proper way to do this in AntD. So not necessarily fixing the approach I'm trying to get to work.

I've been trying a variety of approaches to solve this, but none ever seem to work for me. For example, I've tried:

  1. Use onChange and create a normalizeInput function as outlined here.
  2. After thinking to myself that I shouldn't need to reinvent the wheel and there must be an npm package that I can simply leverage, I tried installing and using react-number-format. It was formatting the field and contents the way that I wanted but I lost the AntD UI (the input field no longer looked like an AntD input field).
  3. Tried playing with a Form's getFieldsValue and setFieldsValue tags but got nowhere fast. I probably just didn't know what I was doing.
  4. Tried using AntD's InputNumber function instead once i saw it had a formatter field that'd do the trick. But I abandoned that approach given I didn't want the up and down arrow controls that e with an InputNumber widget.

Currently #1 sort of works but not quite and I'm not sure why. It uses onChange to read in the current value, and adjust it accordingly, but there are two problems..

  1. previousValue bees undefined on the 6th, 8th, and 10th digit. Why? You'll see it in console.log in my codesandbox below.
  2. The UI's field never sets using the value tag. Why? I can see the value is changing via console.log, but would've thought the setState function would've set the state within onChange, and therefore the field would show the formatted version due to the value tag..?

Here is my codesandbox illustrating the problem (MyForm.js file).

I have an AntD form with an Input field for phone numbers, and another to capture SSN's. As a user types a phone number or SSN, I would like the fields to format accordingly.

For example, instead of the phone number being 1234567890 as typed into the field, it should format as (123) 456-7890 with it initially being (___) ___-____. Similarly, in the SSN field if a user types their SSN such as 123456789 it should format as 123-45-6789 with it initially being ###-##-####.

What I'm really looking for is the proper way to do this in AntD. So not necessarily fixing the approach I'm trying to get to work.

I've been trying a variety of approaches to solve this, but none ever seem to work for me. For example, I've tried:

  1. Use onChange and create a normalizeInput function as outlined here.
  2. After thinking to myself that I shouldn't need to reinvent the wheel and there must be an npm package that I can simply leverage, I tried installing and using react-number-format. It was formatting the field and contents the way that I wanted but I lost the AntD UI (the input field no longer looked like an AntD input field).
  3. Tried playing with a Form's getFieldsValue and setFieldsValue tags but got nowhere fast. I probably just didn't know what I was doing.
  4. Tried using AntD's InputNumber function instead once i saw it had a formatter field that'd do the trick. But I abandoned that approach given I didn't want the up and down arrow controls that e with an InputNumber widget.

Currently #1 sort of works but not quite and I'm not sure why. It uses onChange to read in the current value, and adjust it accordingly, but there are two problems..

  1. previousValue bees undefined on the 6th, 8th, and 10th digit. Why? You'll see it in console.log in my codesandbox below.
  2. The UI's field never sets using the value tag. Why? I can see the value is changing via console.log, but would've thought the setState function would've set the state within onChange, and therefore the field would show the formatted version due to the value tag..?

Here is my codesandbox illustrating the problem (MyForm.js file).

Share Improve this question edited Dec 31, 2020 at 23:23 Gerald Wichmann asked Dec 31, 2020 at 22:37 Gerald WichmannGerald Wichmann 4572 gold badges9 silver badges25 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

check this https://github./antoniopresto/antd-mask-input

is a library to make it. The ponent is

<MaskedInput mask="11/1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>

where you give the prop "mask", where you can make the format.

in your case it would be

<MaskedInput mask="(111) 111-1111" name="expiry" placeholder="mm/yyyy" onChange={this._onChange}/>
发布评论

评论列表(0)

  1. 暂无评论