Formatting a UITextField for credit card input like (xxxx xxxx xxxx xxxx)

前端 未结 28 2196
长情又很酷
长情又很酷 2020-11-28 01:19

I want to format a UITextField for entering a credit card number into such that it only allows digits to be entered and automatically inserts spaces so that the

28条回答
  •  广开言路
    2020-11-28 01:59

    Found a GIST in Github that does exactly what I need in Swift3 (https://gist.github.com/nunogoncalves/6a8b4b21f4f69e0fc050190df96a1e56)

    Implemented by doing ->

    if creditCardNumberTextView.text?.characters.first == "3" {
        let validator = Validator(cardType: .americanExpress, value:  self.creditCardNumberTextView.text!).test()
    
          if validator == true {
    
           } else {
    
           }
       }
    

    Works wonderfully in the APP I'm working out which uses credit cards.

提交回复
热议问题