I have added UITextField in UIAlertView using UIAlertViewStylePlainTextInput. I need to validate the textfield present in alertview i.e it should not be empty
How should i do?
Let us assume that you have an "OK" button (or something similar) as the first of the other buttons of the UIAlertView. Further assume that you want that button to be enabled if and only if the length of text in text field is greater than 0. Then the solution to validation is simple. In the delegate of the UIAlertView implement:
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
return [[alertView textFieldAtIndex:0].text length] > 0;
}
This has the advantage over some of the other answers (which use clickedButtonAtIndex:), that the user is immediate aware of whether the text field contains valid input.
This delegate message is not terribly well explained in Apple's documentation, but it works very well. Any changes to the value of the text field will cause this message to be sent, and the "OK" button to be enabled or disabled accordingly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With