Skip to content

Instantly share code, notes, and snippets.

@kyleclegg
Last active March 28, 2020 00:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kyleclegg/4687811 to your computer and use it in GitHub Desktop.
Save kyleclegg/4687811 to your computer and use it in GitHub Desktop.
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;
}
else
{
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