No release in over 3 years
An ActiveModel extension that automatically converts all non-English digits to English digits before validation. https://github.com/aalyahya/normalize_digits
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 0.17.1
~> 2.0
~> 13.0
~> 7.1.0

Runtime

>= 3.0, < 9.0
 Project Readme

NormalizeDigits

Gem Version

NormalizeDigits is a lightweight ActiveModel extension that automatically converts non-English digits (like Arabic, Persian, etc.) to standard English digits (0-9) before validation. This is useful for applications where users might input localized numerals, and consistent numeric formatting is required.

Features

  • Automatically normalizes any non-English digits to ASCII numerals.
  • Integrates seamlessly with ActiveModel or ActiveRecord attributes.
  • Useful for form inputs, APIs, and user-generated content.

Installation

Add this line to your application's Gemfile:

gem 'normalize_digits'

And then execute:

bundle install

Or install it yourself as:

gem install normalize_digits

Usage

Simply include the concern in your model and specify which attributes you want to normalize:

class User < ApplicationRecord
  include NormalizeDigits

  normalize_digits
  # normalize_digits only: [:username]
  # normalize_digits except: [:username]
end

Before validation, any non-English digits in the specified fields will be converted to standard 0-9.

Example

user = User.new(phone_number: "٠٥٠١٢٣٤٥٦٧")
user.valid?
user.phone_number
# => "0501234567"

Supported Digit Sets

The gem currently supports: • Arabic-Indic digits: ٠١٢٣٤٥٦٧٨٩ • Eastern Arabic digits: ۰۱۲۳۴۵۶۷۸۹ • Can be extended easily via the matchers.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/your-username/normalize_digits.

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/awesome)
  3. Commit your changes (git commit -am 'Add awesome feature')
  4. Push to the branch (git push origin feature/awesome)
  5. Create a new Pull Request

License

The gem is available as open source under the terms of the MIT License.