Advertisement
frenky666

oib - groovy

Feb 17th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 0.42 KB | None | 0 0
  1. class CustomValidators {
  2.   static OIB_VALIDATOR = { String value ->
  3.     if (!(value =~ /^[0-9]{11}$/)) {
  4.       return false
  5.     }
  6.  
  7.     Integer a = 10;
  8.     10.times {
  9.       a += (value[it] as Integer)
  10.       a %= 10
  11.       a = (a == 0 ? 10 : a)
  12.       a *= 2
  13.       a %= 11
  14.     }
  15.  
  16.     def checkDigit = 11 - a
  17.     checkDigit = (checkDigit == 10 ? 0 : checkDigit)
  18.  
  19.     return checkDigit == (value[-1] as Integer)
  20.   }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement