0

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];
            }
        }
George Friday
  • 585
  • 1
  • 6
  • 22
  • Use three different textfields for day, month, and year, and provide the user with the number-pad keyboard for each textfield. – Charles Nov 24 '13 at 23:14
  • Why don't you want to use a date picker? – jscs Nov 24 '13 at 23:15
  • I heard that my app will be rejected if I use date picker for UIAlertViews. Is that true? If not, how do I implement a date picker for this? – George Friday Nov 24 '13 at 23:20
  • See [IOS 7 UIDatePicker in UIAlertView customisation](http://stackoverflow.com/q/18894653) and [How to add a UIDatePicker to UIAlertView in iOS 7?](http://stackoverflow.com/q/18975484) – jscs Nov 24 '13 at 23:26

0 Answers0