Home > formtastic_masked_input

formtastic_masked_input

Formtastic_masked_input is a project mainly written in JAVASCRIPT and RUBY, based on the MIT license.

MaskedInput (JS) for Formtastic

h1. formtastic_masked_input

Sometimes you want to mask your fields like phones, zipcodes or something like that.

This plugin will do it for you if you are using formtastic.

Note: the javascript file depends on Prototype 1.6.1 and Rails uses Prototype version 1.6.0.3, just update your prototype.js

h2. Installation

h3. Plugin

$ ruby script/plugin install git://github.com/sobrinho/formtastic_masked_input.git
$ ruby script/generate formtastic_masked_input

h3. Gem

$ gem install formtastic_masked_input

Add gem dependency to your environment.rb:

config.gem 'formtastic_masked_input'

And generate the masked_input.js:

$ ruby script/generate formtastic_masked_input

h2. Usage

Include prototype.js and masked_input.js into layout:

<%= javascript_include_tag 'prototype', 'masked_input' %>

And update your form field:

<% semantic_form_for @person do |f| %>
  <% f.inputs do %>
    <%= f.input :name %>
    <%= f.input :phone, :as => :masked, :mask => :phone %>
  <% end %>

  <%= f.buttons %>
<% end %>

h2. Masks

The available masks are made for Brazil:

  • phone: (99) 9999-9999
  • cpf: 999.999.999-99
  • cnpj: 99.999.999/9999-99
  • date: 99/99/9999
  • cep: 99999-999
  • time: 99:99

If you want to add a custom mask, you can edit masked_input.js around line 301 (preferred) or add directly into your layout:

<%= javascript_include_tag 'prototype', 'masked_input' %>

h2. Credits

Special thanks to "Justin French":http://github.com/justinfrench for "formtastic":http://github.com/justinfrench/formtastic and "Bjarte K. Vebjørnsen":http://github.com/bjartekv for "MaskedInput port for Prototype":http://github.com/bjartekv/MaskedInput.

h2. TODO

  • Make tests for plugin
  • Make tests for MaskedInput

Copyright (c) 2010 Gabriel Sobrinho, released under the MIT license

Previous:jsNetTools