Class: OmgValidator::Validators::AlphaNumericValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/omg_validator/validators/alpha_numeric_validator.rb

Overview

Checks whether input only contains alpha-numeric characters

Matches:

alpha23, Testing, 343, hello

Does not match:

324-343, alpha-dash, id=343, (232)

Examples:

validates :title, alpha_numeric: true

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/omg_validator/validators/alpha_numeric_validator.rb', line 14

def validate_each(record, attribute, value)
  return nil if value.blank?
  reg = /^([a-z0-9])+$/i
  unless reg.match(value)
    record.errors[attribute] = "must contain only alpha-numeric characters"
  end
end