Skip to content

Instantly share code, notes, and snippets.

@cbueno
Created September 24, 2012 16:27
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cbueno/3776844 to your computer and use it in GitHub Desktop.
Save cbueno/3776844 to your computer and use it in GitHub Desktop.
EN: Objective-C class for validate CPF number / PT-BR: Classe em Objective-C para validação de CPF
Copyright (C) 2012 Cristóferson Bueno
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Validations v1.0.0
//
// Copyright (c) 2012 Cristóferson Bueno.
// Licensed under the MIT.
// Read MIT-LICENSE.txt
//
#import <Foundation/Foundation.h>
@interface Validations : NSObject
+ (BOOL)validateCPFWithNSString:(NSString *)cpf;
@end
//
// Validations v1.0.0
//
// Copyright (c) 2012 Cristóferson Bueno.
// Licensed under the MIT.
// Read MIT-LICENSE.txt
//
#import "Validations.h"
@implementation Validations
+ (BOOL)validateCPFWithNSString:(NSString *)cpf {
NSUInteger i, firstSum, secondSum, firstDigit, secondDigit, firstDigitCheck, secondDigitCheck;
if(cpf == nil) return NO;
if ([cpf length] != 11) return NO;
if (([cpf isEqual:@"00000000000"]) || ([cpf isEqual:@"11111111111"]) || ([cpf isEqual:@"22222222222"])|| ([cpf isEqual:@"33333333333"])|| ([cpf isEqual:@"44444444444"])|| ([cpf isEqual:@"55555555555"])|| ([cpf isEqual:@"66666666666"])|| ([cpf isEqual:@"77777777777"])|| ([cpf isEqual:@"88888888888"])|| ([cpf isEqual:@"99999999999"])) return NO;
firstSum = 0;
for (i = 0; i <= 8; i++) {
firstSum += [[cpf substringWithRange:NSMakeRange(i, 1)] intValue] * (10 - i);
}
if (firstSum % 11 < 2)
firstDigit = 0;
else
firstDigit = 11 - (firstSum % 11);
secondSum = 0;
for (i = 0; i <= 9; i++) {
secondSum = secondSum + [[cpf substringWithRange:NSMakeRange(i, 1)] intValue] * (11 - i);
}
if (secondSum % 11 < 2)
secondDigit = 0;
else
secondDigit = 11 - (secondSum % 11);
firstDigitCheck = [[cpf substringWithRange:NSMakeRange(9, 1)] intValue];
secondDigitCheck = [[cpf substringWithRange:NSMakeRange(10, 1)] intValue];
if ((firstDigit == firstDigitCheck) && (secondDigit == secondDigitCheck))
return YES;
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment