class HInputValidator

Go to rubular.com to try the regex strings

Public Class Methods

isEmail?(input) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/hmisc/hinputvalidator.rb, line 13
def self.isLengthValid?(input)

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

end
isMandatory?(input) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

isValid(“hb@gmail.com”, “email,unique”, “email”, “users”)

# 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) click to toggle source
# 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) click to toggle source
# 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