Created
February 13, 2018 12:37
-
-
Save themakunga/9b6e354b0cc10ebd9e2e14f9920b8059 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
////// | |
// | |
// funcion para validar DV en rut chileno, | |
// Swift 4 Xcode 9.x | |
// | |
// | |
// Modo de uso: | |
do{ | |
let validator = try FormValidator.validateRut("16242343-5") //cambiar por string de rut a validar | |
}catch RutError.InvalidRut(let message) { | |
showMessage("Error", message: message, action: nil) | |
}catch{ | |
DDLogError("an error ocurred during rut validation") | |
} | |
// | |
enum RutError: ErrorType{ | |
case InvalidRut(message : String) | |
} | |
class FormValidator: NSObject { | |
static func validadorRut(input: String!) throws -> String?{ | |
var rut = input.replacingOccurrences(of: ".", with: "", options: NSString.CompareOptions.literal, range : nil) | |
rut = rut.replacingOccurrences(of: "-", with: "", options: NSString.CompareOptions.literal, range : nil) | |
rut = rut.uppercased() | |
if (rut.count < 8 || rut.count > 10){ | |
throw rutErrors.invalidRut(message: "Largo de RUT no válido") | |
} | |
let rutRegex = "^(\\d{1,3}(\\.?\\d{3}){2})\\-?([\\dkK])$" | |
let rutTest = NSPredicate(format: "SELF MATCHES %@", rutRegex) | |
if (!rutTest.evaluate(with: rut)) { | |
throw rutErrors.invalidRut(message: "RUT con fomato no válido") | |
} | |
let runTovalidate = getRutDv(value: input) | |
let rutNumber = runTovalidate.0 | |
let rutDV = runTovalidate.1 | |
let calculatedDV = validateDV(rut: rutNumber) | |
print(runTovalidate) | |
print(calculatedDV!) | |
if (rutDV != calculatedDV){ | |
throw rutErrors.invalidRut(message: "Digito verificador no válido") | |
} | |
return rut | |
} | |
static func getRutDv(value: String) -> (String, String){ | |
if (value.isEmpty || value.count < 2){ | |
return ("", "") | |
} | |
var rut = value.replacingOccurrences(of: ".", with: "", options: NSString.CompareOptions.literal, range: nil) | |
rut = rut.replacingOccurrences(of: "-", with: "", options: NSString.CompareOptions.literal, range: nil) | |
let dv: String = String(rut.last!) | |
let run: String = String(rut.dropLast()) | |
return (run, dv) | |
} | |
static func validateDV(rut: String) -> String?{ | |
var acumulado : Int = 0 | |
var ti : Int = 2 | |
print(rut.count) | |
for index in stride(from: rut.count-1, through: 0, by: -1) { | |
let n = Array(rut)[index] | |
let nl = String(n) | |
guard let vl = Int(nl) else { | |
return "" | |
} | |
acumulado += vl * ti | |
if (ti == 7) { | |
ti = 1 | |
} | |
ti += 1 | |
} | |
let resto : Int = acumulado % 11 | |
let numericDigit : Int = 11 - resto | |
var digito : String = "" | |
if (numericDigit <= 9){ | |
digito = String(numericDigit); | |
}else if (numericDigit == 10){ | |
digito = "K" | |
}else{ | |
digito = "0" | |
} | |
return digito | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment