输入字符串的验证是否为日期? [英] Validation for input string is Date or not?
本文介绍了输入字符串的验证是否为日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个文本字段,用户可以在其中输入他们的出生日期,现在我想在保存到数据库之前确定输入是否为日期格式.我通过了 SO ,但仍然没有得到答案.谁能告诉我如何验证(它是日期)输入.
I have a textfield where the user can enter their date of birth, now i wants to make sure whether the input is of date format before saving into database. I gone through the SO , but still i didn't get the answer. Can anyone tell me how to validate(is it date) the input.
日期格式为 MM/DD/YYYY
注意:我不希望用户通过日期选择器选择日期.
推荐答案
试试这个
NSString *dateFromTextfield = @"07/24/2013";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy"];// here set format which you want...
NSDate *date = [dateFormat dateFromString:dateFromTextfield];
[dateFormat release];
然后检查
//set int position to 2 and 5 as it contain / for a valid date
unichar ch = [dateFromTextfield characterAtIndex:position];
NSLog(@"%c", ch);
if (ch == '/')
{
//valid date
}
else
{
//not a valid date
}
这篇关于输入字符串的验证是否为日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文