Sha256: b7a6cd830c3c347f8c3594652a2983d7643ca5e29f29bd08b0fab04d14349f8b

Contents?: true

Size: 788 Bytes

Versions: 11

Compression:

Stored size: 788 Bytes

Contents

class AlphaNumericValidator < ActiveModel::EachValidator

  def validate_each(record, attribute, value)
    unless valid?(value, options)
      record.errors[attribute] << (options[:message] || I18n.t('flash_validators.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

11 entries across 11 versions & 1 rubygems

Version Path
flash_validators-3.0.12 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.11 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.10 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.9 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.8 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.7 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.6 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.5 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.4 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.3 lib/flash_validators/validators/alpha_numeric_validator.rb
flash_validators-3.0.2 lib/flash_validators/validators/alpha_numeric_validator.rb