# Validate input on alertcontroller using Xcode/objective C

**URL:** <https://forums.kodeco.com/t/validate-input-on-alertcontroller-using-xcode-objective-c/29394>\
**Category:** iOS\
**Created:** [May 8, 2017, 3:51pm UTC](https://forums.kodeco.com/t/validate-input-on-alertcontroller-using-xcode-objective-c/29394 "2017-05-08T15:51:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmantrip](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/kmantrip/32/66317_2.png) [@kmantrip](https://forums.kodeco.com/u/kmantrip)\
**Post date:** [May 8, 2017, 3:51pm UTC](https://forums.kodeco.com/t/validate-input-on-alertcontroller-using-xcode-objective-c/29394/1 "2017-05-08T15:51:18Z")

</div>

I am showing an alert on button click, which has a text field and OK/cancel buttons. I like ‘OK’ button to be displayed only if the user types in the text box.

Here is my code: I setup a function on text change. But it is not working as I intend to. Can anyone see what I am doing wrong.  
appreciate your help.

(IBAction)displayAlertview:(id)sender{

```
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                                                               message:@"This is an alert."
                                                        preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction* okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {}];

UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel
                                                      handler:^(UIAlertAction * action) {}];

[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
    
    [textField addTarget:self action:@selector(alertControllerTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
}];

      okAction.enabled = false;
[alert addAction:okAction];
[alert addAction:cancelAction];

[self presentViewController:alert animated:YES completion:nil];

```

}

-(void)alertControllerTextFieldDidChange:(UITextField \*)sender{  
UIAlertController \*alertController = (UIAlertController \*)self.presentedViewController;  
if (alertController) {  
UITextField \*someTextField = alertController.textFields.firstObject;  
UIAlertAction \*okAction = alertController.actions.lastObject;  
okAction.enabled = true; [//someTextField.text.length](https://someTextField.text.length) \>2;  
}

}
