I have an alertview textfield in which a user should enter a date. How can I specify that only the date format of dd/mm/yyyy is allowed in this textfield? I am trying to achieve this without having to use a date picker.
So far, I am able to specify that only numbers and slashes are allowed. Here is my code for this:
if (buttonIndex != alertView.cancelButtonIndex)
{
UITextField *fieldDate = [alertView textFieldAtIndex:0];
fieldDate.placeholder = @"Enter New Date";
NSCharacterSet * setnew = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789/"] invertedSet];
if ([fieldDate.text rangeOfCharacterFromSet:setnew].location != NSNotFound)
{
//show alert view here
UIAlertView *errorAlert1 = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Only numbers and slashes are allowed in this field."delegate:self cancelButtonTitle:@"OK"otherButtonTitles:nil];
[errorAlert1 show];
FeeDate.text=@"00/00/0000";
}
else
{
FeeDate.text = fieldDate.text;
[[NSUserDefaults standardUserDefaults] setObject:fieldDate.text forKey:DATE_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}