Class: HInputValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/hmisc/hinputvalidator.rb

Overview

Go to rubular.com to try the regex strings

Class Method Summary collapse

Class Method Details

.isEmail?(input) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
# File 'lib/hmisc/hinputvalidator.rb', line 55

def self.isEmail?(input)

  regex = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i
  return !(input !~ regex) && self.isValid?(input)

end

.isInteger?(input) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/hmisc/hinputvalidator.rb', line 41

def self.isInteger?(input)
  
  regex = /\A-?[0-9]+\z/
  return !(input !~ regex) && self.isValid?(input)

end

.isLengthValid?(input) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'lib/hmisc/hinputvalidator.rb', line 13

def self.isLengthValid?(input)

  regex = /\A.{0,40}\z/
  return !(input !~ regex)

end

.isMandatory?(input) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
# File 'lib/hmisc/hinputvalidator.rb', line 20

def self.isMandatory?(input)
  
  regex = /\A.{1,500}\z/
  return !(input !~ regex) && self.isValid?(input)

end

.isNatural?(input) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
# File 'lib/hmisc/hinputvalidator.rb', line 48

def self.isNatural?(input)
  
  regex = /\A[0-9]+\z/
  return !(input !~ regex) && self.isValid?(input)

end

.isPassword?(input) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
# File 'lib/hmisc/hinputvalidator.rb', line 27

def self.isPassword?(input)
  
  regex = /^[\w.@!$\-]{5,}$/  # at least 5 character, are allow the follows special chars: . @ ! $ -
  return !(input !~ regex) && self.isValid?(input)

end

.isReal?(input) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
# File 'lib/hmisc/hinputvalidator.rb', line 34

def self.isReal?(input)
  
  regex = /^-?[0-9]\d*(\.\d+)?$/
  return !(input !~ regex) && self.isValid?(input)

end

.isUnique?(fieldValue, fieldName, modelName) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
67
# File 'lib/hmisc/hinputvalidator.rb', line 62

def self.isUnique?(fieldValue, fieldName, modelName)

  result = HSqlTable.new().loadFromQuery("select count(*) from #{modelName} where #{fieldName} = '#{fieldValue}'")[0][0]
  return (result == "1") && self.isValid?(fieldValue)

end

.isValid(fieldValue, fieldType, fieldName, modelName) ⇒ Object

isValid(“[email protected]”, “email,unique”, “email”, “users”)



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hmisc/hinputvalidator.rb', line 70

def self.isValid(fieldValue, fieldType, fieldName, modelName)

  result = true
  types = fieldType.split(",")
  types.each do |type|
    type = type.strip()
    result &&= self.isLengthValid?(fieldValue) if (type == "length")
    result &&= self.isMandatory?(fieldValue) if (type == "mandatory")
    result &&= self.isPassword?(fieldValue) if (type == "password")
    result &&= self.isReal?(fieldValue) if (type == "real")
    result &&= self.isInteger?(fieldValue) if (type == "integer")
    result &&= self.isNatural?(fieldValue) if (type == "natural")
    result &&= self.isEmail?(fieldValue) if (type == "email")
    result &&= self.isUnique?(fieldValue, fieldName, modelName) if (type == "unique")
  end

  return (result) ? "true": "false"

end

.isValid?(input) ⇒ Boolean

General Validation

Returns:

  • (Boolean)


7
8
9
10
11
# File 'lib/hmisc/hinputvalidator.rb', line 7

def self.isValid?(input)  # General Validation
  
  return self.isLengthValid?(input)

end

.valid?(fieldValue: nil, fieldType: nil, fieldName: nil, modelName: nil) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/hmisc/hinputvalidator.rb', line 90

def self.valid?(fieldValue: nil, fieldType: nil, fieldName: nil, modelName: nil)
  return self.isValid(fieldValue, fieldType, fieldName, modelName)
end