[SOLVED] Set UITextField to mm-dd-yy format in swift

Issue

So I want to have a UITextField to only accept digits, solved that by using a custom keyboard input.

The intention of this UITextField is to get someones birthday. I don’t want to use an UIDatePicker tough as I don’t like it’s appearance.

I’d like that the TextField automatically inserts dashes after every second digit that the user put into the TextField.

dd-mm-yy is the placeholder text. I either thought of making the dashes permanently but I don’t know how to do that either.

How can I do this?

Solution

You want to allow user to enter text in textfield in this dd-mm-yy right ?
if it so i’am sure this will help you.

  1. In top of your class declare this variable which we gonna use later.

    var dateFormate = Bool()
    
  2. Add delegate and tag for that textfield.

  3. Then add this following delegate method

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
    //1. To make sure that this is applicable to only particular textfield add tag.
    if textField.tag == 1 {
        //2. this one helps to make sure that user enters only numeric characters and '-' in fields
        let numbersOnly = NSCharacterSet(charactersInString: "1234567890-")
        let characterSetFromTextField = NSCharacterSet(charactersInString: string)
    
        let Validate:Bool = numbersOnly .isSupersetOfSet(characterSetFromTextField)
        if !Validate {
            return false;
        }
        if range.length + range.location > textField.text?.characters.count {
            return false
        }
        let newLength = (textField.text?.characters.count)! + string.characters.count - range.length
        if newLength == 3 || newLength == 6 {
            let  char = string.cStringUsingEncoding(NSUTF8StringEncoding)!
            let isBackSpace = strcmp(char, "\\b")
    
            if (isBackSpace == -92) {
                dateFormate = false;
            }else{
                dateFormate = true;
            }
    
            if dateFormate {
                let textContent:String!
                textContent = textField.text
                //3.Here we add '-' on overself.
                let textWithHifen:NSString = "\(textContent)-"
                textField.text = textWithHifen as String
                dateFormate = false
            }
        }
        //4. this one helps to make sure only 8 character is added in textfield .(ie: dd-mm-yy)
        return newLength <= 8;
    
    }
    return true
    }
    
  4. That’s it now user can enter their DOB.No need to worry about ‘-‘ it will be added automatically.

Swift 3:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    //1. To make sure that this is applicable to only particular textfield add tag.
    if textField.tag == 1 {
        //2. this one helps to make sure that user enters only numeric characters and '-' in fields
        let numbersOnly = CharacterSet(charactersIn: "1234567890-")

        let Validate = string.rangeOfCharacter(from: numbersOnly.inverted) == nil ? true : false
        if !Validate {
            return false;
        }
        if range.length + range.location > textField.text?.characters.count {
            return false
        }
        let newLength = (textField.text?.characters.count)! + string.characters.count - range.length
        if newLength == 3 || newLength == 6 {
            let  char = string.cString(using: NSUTF8StringEncoding)!
            let isBackSpace = strcmp(char, "\\b")

            if (isBackSpace == -92) {
                dateFormate = false;
            }else{
                dateFormate = true;
            }

            if dateFormate {
                let textContent:String!
                textContent = textField.text
                //3.Here we add '-' on overself.
                let textWithHifen:NSString = "\(textContent)-"
                textField.text = textWithHifen as String
                dateFormate = false
            }
        }
        //4. this one helps to make sure only 8 character is added in textfield .(ie: dd-mm-yy)
        return newLength <= 8;

    }
    return true
}

Answered By – Gokul G

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.