Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Marks invalid UITextFields red in iOS. If valid, restores original border.
#import <QuartzCore/QuartzCore.h>
// Form validation. You'll probably place this in your buttonClicked action
if ([textField.text isEqualToString:@""]) // checks if field is empty, add other form validation here
textField.layer.cornerRadius = 8.0f;
textField.layer.masksToBounds = YES;
textField.layer.borderColor = [[UIColor redColor] CGColor];
textField.layer.borderWidth = 1.0f;
textField.layer.borderColor = [[UIColor clearColor] CGColor];
// To check if valid after each character is entered, implement UITextField delegate
// in your interface file and add this code to your implementation file
#pragma mark - UITextField Delegate
- (BOOL) textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)textEntered {
// Do not check if the textField was previously blank
if (![textField.text isEqualToString:@""]) {
// perform from validation here instead of on button click
return YES;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.