Class: ActiveModel::Validations::OnlyAlphaNumericValidator

Inherits:
EachValidator
  • Object
show all
Defined in:
lib/moca_rlibs/active_model_validators/only_alpha_numeric.rb

Overview

文字列が半角英数字のみであることをチェックする

Instance Method Summary collapse

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/moca_rlibs/active_model_validators/only_alpha_numeric.rb', line 9

def validate_each(record, attribute, value)
  return if value.blank?

  value = value.presence.to_s
  return if value.match?(/\A[a-zA-Z0-9]*\z/)

  record.errors.add('contain invalid characters (only numbers and alphabets)')
end