Class: MxitRails::Validations
- Inherits:
-
Object
- Object
- MxitRails::Validations
- Defined in:
- lib/mxit_rails/validations.rb
Class Method Summary collapse
- .cellphone_number?(input) ⇒ Boolean
- .length?(input, len) ⇒ Boolean
- .max_length?(input, max) ⇒ Boolean
- .max_value?(input, max) ⇒ Boolean
- .min_length?(input, max) ⇒ Boolean
- .min_value?(input, min) ⇒ Boolean
- .not_blank?(input) ⇒ Boolean
- .numeric?(input) ⇒ Boolean
- .sa_id_number?(input) ⇒ Boolean
Class Method Details
.cellphone_number?(input) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mxit_rails/validations.rb', line 32 def self.cellphone_number? input return false unless numeric?(input) # Normalise to local 0xx format input.sub! /^00/, '0' input.sub! /^\+/, '' input.sub! /^27/, '0' # Check length is 10, and first digits are 07 or 08 return false unless length?(input, 10) return false unless ((input =~ /^07/) || (input =~ /^08/)) return true end |
.length?(input, len) ⇒ Boolean
12 13 14 |
# File 'lib/mxit_rails/validations.rb', line 12 def self.length? input, len return !input.blank? && (input.length == len) end |
.max_length?(input, max) ⇒ Boolean
20 21 22 |
# File 'lib/mxit_rails/validations.rb', line 20 def self.max_length? input, max return input.blank? || (input.length <= max) end |
.max_value?(input, max) ⇒ Boolean
28 29 30 |
# File 'lib/mxit_rails/validations.rb', line 28 def self.max_value? input, max return input.to_f <= max end |
.min_length?(input, max) ⇒ Boolean
16 17 18 |
# File 'lib/mxit_rails/validations.rb', line 16 def self.min_length? input, max return !input.blank? && (input.length >= max) end |
.min_value?(input, min) ⇒ Boolean
24 25 26 |
# File 'lib/mxit_rails/validations.rb', line 24 def self.min_value? input, min return input.to_f >= min end |
.not_blank?(input) ⇒ Boolean
4 5 6 |
# File 'lib/mxit_rails/validations.rb', line 4 def self.not_blank? input return !input.blank? end |
.numeric?(input) ⇒ Boolean
8 9 10 |
# File 'lib/mxit_rails/validations.rb', line 8 def self.numeric? input return !input.blank? && input.match(/^[0-9]+$/) end |
.sa_id_number?(input) ⇒ Boolean
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/mxit_rails/validations.rb', line 47 def self.sa_id_number? input return false unless numeric?(input) #1. numeric and 13 digits return false unless length?(input, 13) #2. first 6 numbers is a valid date dateString = "#{input[0..1]}-#{input[2..3]}-#{input[4..5]}" begin date = Date.parse dateString rescue ArgumentError return false end #3. luhn formula temp_total = 0 checksum = 0 multiplier = 1 (0..12).each do |i| temp_total = input[i].to_i * multiplier if temp_total > 9 temp_total = temp_total.to_s[0].to_i + temp_total.to_s[1].to_i end checksum += temp_total multiplier = multiplier.even? ? 1 : 2 end return checksum % 10 == 0 end |