What I want to implement is, TextField format should be XXXX XXXX XXXX when the user clicks on the show button it should show the text 1234 5678 9123
Below is my code where I was able to implement the 1234 5678 9123 format, masking and storing original data is pending.
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool
{
if textField.tag == 0 || textField.tag == 1 {
if string.isBackspace
{
if range.location == 5 || range.location == 10
{
textField.text?.removeLast()
}
}
if string == ""{
return true
}
//range.length will be greater than 0 if user is deleting text - allow it to replace
if range.length > 0
{
return true
}
//Don't allow empty strings
if string == " "
{
return false
}
//Check for max length including the spacers we added
if range.location > 13 //23
{
return false
}
var originalText = textField.text
let replacementText = string.replacingOccurrences(of: " ", with: "")
//Verify entered text is a numeric value
let digits = NSCharacterSet.decimalDigits
for char in replacementText.unicodeScalars
{
if !(digits as NSCharacterSet).longCharacterIsMember(char.value)
{
return false
}
}
//Put an empty space after every 4 places
if (originalText?.count)! > 0
{
if (originalText?.count)! < 5 && (originalText?.count)! % 4 == 0{
originalText?.append(" ")
}else if(((originalText?.count)! + 1) % 5 == 0){
originalText?.append(" ")
}
}
textField.text = originalText
}
//2 - Nick Name,3 - Recipient Name
if textField.tag == 2 || textField.tag == 3 {
//Max Limit for Nick Name and Benificiary Name is 65
if range.location > 64
{
return false
}
}
return true
}