Email validation on textField in iOS

Use NSPredicate and Regex: – (BOOL)validateEmailWithString:(NSString*)email { NSString *emailRegex = @”[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}”; NSPredicate *emailTest = [NSPredicate predicateWithFormat:@”SELF MATCHES %@”, emailRegex]; return [emailTest evaluateWithObject:email]; } For a bunch of emails separated by a comma: – (NSMutableArray*)validateEmailWithString:(NSString*)emails { NSMutableArray *validEmails = [[NSMutableArray alloc] init]; NSArray *emailArray = [emails componentsSeparatedByString:@”,”]; for (NSString *email in emailArray) { NSString *emailRegex = … Read more

Unable to add UITextField to UIAlertView on iOS7…works in iOS 6

You can’t easily alter the view hierarchy of a UIAlertView in iOS 7. (Nor should you; the documentation specifically tells you not to.) Head over to the developer forums to see a long discussion about it. One alternative in your case is to set alert.alertViewStyle = UIAlertViewStylePlainTextInput; This will add a text field for you. … Read more

How to dismiss keyboard iOS programmatically when pressing return

The simple way is to connect the delegate of UITextField to self (self.mytestField.delegate = self) and dismiss the keyboard in the method textFieldShouldReturn using [textField resignFirstResponder]; Another way to dismiss the keyboard is the following: Objective-C [self.view endEditing:YES]; Swift: self.view.endEditing(true) Put [self.view endEditing:YES]; where you would like to dismiss the keyboard (Button event, Touch event, … Read more

Swift – validating UITextField

Alternatively, you can use this, which is called every time a key is pressed: name1.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name2.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name3.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) name4.addTarget(self, action: “textFieldDidChange:”, forControlEvents: UIControlEvents.EditingChanged) func textFieldDidChange(textField: UITextField) { if name1.text?.isEmpty || name2.text?.isEmpty || name3.text?.isEmpty || name4.text?.isEmpty { //Disable button } else { //Enable button } … Read more

self.delegate = self; what’s wrong in doing that?

See this thread http://www.cocoabuilder.com/archive/cocoa/241465-iphone-why-can-a-uitextfield-be-its-own-delegate.html#241505 Basically, the reason for the “freeze” when you click on your UITextField with itself as a delegate is that respondsToSelector is calling itself -> infinite recursion. UITextField is unique AFAIK. You can usually use a class as its own delegate with no particular problems. For UITextField you must create an actual … Read more