我有一个alertview文本字段,用户应在其中输入日期。如何在此文本字段中指定仅允许dd / mm / yyyy的日期格式?我试图在不使用日期选择器的情况下实现这一目标。
到目前为止,我能够指定只允许使用数字和斜杠。这是我的代码:
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];
}
}