-4

In my app the user enters the time in the UITextField,

I have used the NSDateFormatter to set the time in according to the format YYYY-MM-DD HH:mm:ss.

I want to to check/validate whether time entered in the UITextField so that the user enters the time in HH and mm.

How can I can set the VALIDATION on the UITEXTFIELD input?

Thanks

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154
Sam Shaikh
  • 1,596
  • 6
  • 29
  • 53
  • 1
    Use UIDatePicker for selection...After selected of date your can change the Date Format as per your need and apply it to text field.. This reduces the code of validation..& efficient way! – Vidhyanand Aug 19 '14 at 06:09
  • check this following link http://stackoverflow.com/questions/17827702/validation-for-input-string-is-date-or-not – Pawan Rai Aug 19 '14 at 06:19
  • Thank you, done it through the NSDateFormatter – Sam Shaikh Aug 19 '14 at 06:49

1 Answers1

0

The way you are taking input is not suitable. Time input can be taken by UIDatePicker.

-(IBAction)showDatePicker:(id)sender{

    datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
    datePicker.datePickerMode = UIDatePickerModeTime;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];

    [datePicker addTarget:self
                   action:@selector(LabelChange:)
         forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
}

-(IBAction)buttonPressed:(id)sender
  {


    NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
    [dateformatter setDateFormat:@"HH:mm:ss"];
    NSLog(@"%@",[dateformatter stringFromDate:[datePicker date]]); //here you will get selected Date


 }

You can choose DatePickerMode by any of them according to your requirement.

UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer

NOTE: You can also use NSDateFormatter, and validate If you donot want to use UIDatePicker. In second method you have to add it string instead of date from picker.

Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154