Sha256: 7d3c5330a35e19e3b1aed33832917025098e315934654ba5b7c05ab8ce25a6a6

Contents?: true

Size: 782 Bytes

Versions: 1

Compression:

Stored size: 782 Bytes

Contents

class AlphaNumericValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('yukonisuru.errors.messages.alpha_numeric'))
    end
  end

  private

  def valid_format?(value, options)
    case options[:case]
    when :lower
      options[:strict] ? (value =~ /^[a-z0-9]+$/) : (value =~ /^[a-z0-9 ]+$/)
    when :upper
      options[:strict] ? (value =~ /^[A-Z0-9]+$/) : (value =~ /^[A-Z0-9 ]+$/)
    else
      options[:strict] ? (value =~ /^[A-Za-z0-9]+$/i) : (value =~ /^[A-Za-z0-9 ]+$/i)
    end
  end

  def valid_length?(value)
    value != ' '
  end

  def valid?(value, options)
    valid_format?(value, options) &&
    valid_length?(value)
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
yukonisuru-0.0.1 lib/yukonisuru/validators/alpha_numeric_validator.rb