Skip to content

Instantly share code, notes, and snippets.

@asparagui
Created June 15, 2012 00:31
Show Gist options
  • Save asparagui/2933861 to your computer and use it in GitHub Desktop.
Save asparagui/2933861 to your computer and use it in GitHub Desktop.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
//NSLog(@"textfield hit: %@", textField.placeholder);
if (string.length == 0) {
return YES;
}
if (textField == self.CardholderName) {
return YES;
} else {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
if (textField == self.CardNumber) {
if (newLength > 16) {
return NO;
}
}
if (textField == self.ExpiryDate) {
if (newLength > 4) {
return NO;
}
}
if (textField == self.CCV) {
if (newLength > 4) {
return NO;
}
}
if (textField == self.ZipCode) {
if (newLength > 5) {
return NO;
}
}
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return [string rangeOfCharacterFromSet:nonNumberSet].location == NSNotFound;
}
return NO;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment