Format credit card number

后端 未结 10 2241
梦谈多话
梦谈多话 2020-12-08 11:27

How to format and validate a credit card number with spaces between each 4 digit while typing:

eg: 4464 6846 4354 3564

I have tried:

10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-08 12:22

    Format credit card number will be 16 digits and having automatic spacing between them will get by trying the below code for me. try it once

    handlecard(text) {
        let formattedText = text.split(' ').join('');
        if (formattedText.length <= 16) {
          if (formattedText.length > 0) {
            formattedText = formattedText.match(new RegExp('.{1,4}', 'g')).join(' ');
          }
        } else {
          alert("plz stop here")
        }
        this.setState({ creditCard: formattedText });
        return formattedText;
      }
    

提交回复
热议问题