开发者

How to check date validation in iphone?

I want to add date into my database ..

and I want to add it in some specific format ..like dd/mm/yyyy

how to check the validation for it?

Like e-mail validation .. I w开发者_开发问答ant to check is it in specified format or not?

how to do this ?


1) What is the input method for your date?

  • If it is thru a UIDatePicker, you have nothing to do, the UIDatePicker will return a valid NSDate. I encourage you to manipulate NSDate objects and use UIDatePickers as date inputs especially to avoid the question of date validation.

  • If it is thru a text field, you can set the UITextField's inputView to an UIDatePicker and you won't have to bother about date validation either as you will obviously directly have a (parsed) NSDate that you can manipulate directly, being sure it is valid.

Then in either case, use NSDateFormatter to convert your NSDate (entered by the user thru UIDatePicker) to an NSString (to insert in your database).


2) If you have a plain text entered by the user thru the keyboard, and can't or don't want to use a UIDatePicker (even as the inputView of your UITextField), you can convert your NSString to an NSDate thru an NSDateFormatter to check if it returns a non-nil NSDate (meaning it is a valid date).

Note: You may also use NSRegularExpression or NSPredicate classes to check the format of your string... but you won't have the subtleties of NSDate validation (like checking that 31/02/2011 or 67/13/99 is invalid)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜