Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone validation for textfield added in uialertview

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?

like image 369
duk Avatar asked Jan 18 '12 12:01

duk


1 Answers

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.

like image 198
rene Avatar answered Nov 04 '22 20:11

rene