Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. import Foundation
  2.  
  3. enum RutError: ErrorType{
  4.  
  5. case InvalidRut(message : String)
  6.  
  7. }
  8. class FormValidator: NSObject {
  9. static func validateRut(value : String) throws ->String {
  10. //clean rut
  11. var rut = value.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
  12. rut = rut.stringByReplacingOccurrencesOfString("-", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
  13. rut = rut.uppercaseString
  14. //check length
  15. if (rut.characters.count < 8 || rut.characters.count > 9 ) {
  16. throw RutError.InvalidRut(message: "Rut ingresado no válido")
  17. }
  18. let rutRegEx = "^(\\d{1,3}(\\.?\\d{3}){2})\\-?([\\dkK])$"
  19.  
  20. let rutTest = NSPredicate(format:"SELF MATCHES %@", rutRegEx)
  21.  
  22. if (!rutTest.evaluateWithObject(rut)) {
  23.  
  24. throw RutError.InvalidRut(message: "Rut ingresado no válido")
  25. }
  26.  
  27. let rutToValidate = getRutAndDv(rut)
  28.  
  29. let rutWithoutVerifier = rutToValidate.0
  30. let verifiedDigit = rutToValidate.1
  31.  
  32. let calculatedVerifiedDigit = getVerifiedDigit(rutWithoutVerifier)
  33.  
  34. if (verifiedDigit != calculatedVerifiedDigit) {
  35.  
  36. throw RutError.InvalidRut(message: "Rut ingresado no válido")
  37. }
  38.  
  39. return rut
  40. }
  41. static func getRutAndDv (value : String) -> (String, String) {
  42.  
  43. if (value.isEmpty || value.characters.count < 2) {
  44. return ("","")
  45. }
  46.  
  47. var rut = value.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
  48. rut = rut.stringByReplacingOccurrencesOfString("-", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
  49.  
  50. let verifiedDigit = Array(rut.characters)[rut.characters.count-1]
  51. let range : Range<String.Index> = rut.startIndex..<rut.endIndex.advancedBy(-1)
  52. let rutWithoutVerifiedDigit = rut.substringWithRange(range)
  53.  
  54. return (rutWithoutVerifiedDigit, String(verifiedDigit))
  55. }
  56.  
  57. static func getVerifiedDigit (rut : String) -> String {
  58.  
  59. var acumulado : Int = 0
  60. var ti : Int = 2
  61.  
  62. for index in (rut.characters.count-1).stride(through: 0, by: -1) {
  63. let n = Array(rut.characters)[index]
  64. let nl = String(n)
  65. guard let vl = Int(nl) else {
  66. return ""
  67. }
  68.  
  69. acumulado += vl * ti
  70.  
  71. if (ti == 7) {
  72. ti = 1
  73. }
  74. ti += 1
  75. }
  76.  
  77. let resto : Int = acumulado % 11
  78. let numericDigit : Int = 11 - resto
  79. var digito : String = ""
  80.  
  81. if (numericDigit <= 9){
  82. digito = String(numericDigit);
  83. }else if (numericDigit == 10){
  84. digito = "K"
  85. }else{
  86. digito = "0"
  87. }
  88.  
  89. return digito
  90. }
  91.  
  92.  
  93. }
  94.  
  95.  
  96. //para invocar
  97.  
  98.  
  99. do{
  100. let validator = try FormValidator.validateRut("16242343-5") //cambiar por string de rut a validar
  101.  
  102. }catch RutError.InvalidRut(let message) {
  103.  
  104. showMessage("Error", message: message, action: nil)
  105. }catch{
  106. DDLogError("an error ocurred during rut validation")
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement