I currently have a textfield were a user has to enter their birthday. After they enter their birthday the data is then uploaded to a server. In order to upload to the server the date must be in the format: 07-04-05. I want to set an alert so if it's not in that format an error message will pop up. How can I test to see if the user put it in the correct format?
Asked
Active
Viewed 33 times
-2
-
1It may be a better solution to use a `UIDatePicker` and format yourself the date. Else, you have to use the `UITextFieldDelegate` methods: http://stackoverflow.com/questions/33636756/ios-objective-c-uitextfield-date-formatting – Larme Jul 06 '16 at 19:55
2 Answers
0
Map the text entered in the Text Field to an array of characters:
let enteredStringArray = myTextField.text.characters.map { String($0) }
Once you have the array of characters simply check that the first 2 are integers, the third one is a "-", the next two characters are integers and so on.
If one of these comparisons don't match, display an alert to the user? and prompt the to enter the date with the correct format.

omzy
- 93
- 1
- 10
0
use a regular expression:
let input = "07-04-05"
if input.rangeOfString("^\\d{2}-\\d{2}-\\d{2}$", options: .RegularExpressionSearch) != nil {
print("correct format")
}

André Slotta
- 13,774
- 2
- 22
- 34