Code Monkey home page Code Monkey logo

validates_formatting_of's Introduction

F# validates_formatting_of Build Status

The validates_formatting_of gem adds several convenient methods to validate things such as emails, urls, and phone numbers in a Rails application.

Installation

To install validates_formatting_of, add the following to your Gemfile:

gem 'validates_formatting_of'

Then bundle install:

bundle install

Usage

Using validates_formatting_of is as simple as using Rails' built-in validation methods in models.

class User < ActiveRecord::Base
  validates_formatting_of :email, :using => :email
end

This call will ensure that the user-provided email is a valid email. This way, you will not need to find or write your own regex to validate. All of that logic is contained within validates_formatting_of

Rails validation options still available

You can still add the following options when using validates_formatting_of:

  • :allow_nil
  • :allow_blank
  • :message for custom validation messages

Available Formatting Validations

validates_formatting_of has support for the following validations:

Email

class User < ActiveRecord::Base
  validates_formatting_of :email, :using => :email
end

A valid email consists of at least one alphanumeric or special character (!#$%^&*-_=+`~|), followed by an @ symbol, followed by at least one alphanumeric character or the - symbol, followed by a period (.) followed by two to four alphabetic characters. All characters are case-insensitive.

Examples

[email protected]    #=> valid
[email protected]         #=> valid
[email protected]                #=> valid
!#$%^&*[email protected]  #=> valid

abcd                  #=> invalid
abc@mega$$$.com       #=> invalid
[email protected]              #=> invalid
[email protected]          #=> invalid
[email protected]           #=> invalid

URL

class Sites < ActiveRecord::Base
  validates_formatting_of :website, :using => :url
end

A valid url starts with either http:// or https://, followed by a least one alphanumeric or or dash (-) character, followed by a period (.), followed by at least one alphanumeric or dash (-) character.

Examples

http://abc.com              #=> valid
https://abc.com             #=> valid
http://1.-                  #=> valid
http://sub.domain.longname  #=> valid

abcd                        #=> invalid
http://.co                  #=> invalid

Alpha

class Name < ActiveRecord::Base
  validates_formatting_of :first_name, :using => :alpha
end

Alphanumeric

class Sites < ActiveRecord::Base
  validates_formatting_of :text, :using => :alphanum
end

Credit Card (Visa, Mastercard, Discover, and American Express)

class Purchases < ActiveRecord::Base
  validates_formatting_of :cc, :using => :credit_card
end

US Zipcodes

class Location < ActiveRecord::Base
  validates_formatting_of :zipcode, :using => :us_zip
end

US Phone numbers

class Phones < ActiveRecord::Base
  validates_formatting_of :phone, :using => :us_phone
end

IP Address

class Location < ActiveRecord::Base
  validates_formatting_of :website, :using => :ip_address
end

Social Security Number

class User < ActiveRecord::Base
  validates_formatting_of :ssn, :using => :ssn
end 

Hex Colors

class Color < ActiveRecord::Base
  validates_formatting_of :color, :using => :hex_color
end

Dollar Amount

class Invoice < ActiveRecord::Base
  validates_formatting_of :amount, :using => :dollars
end

Customizable

If, for any reason, you want to use your own regex instead of Rail's built-in methods, you can specify what you want to use with the :regex option. For example,

class Person < ActiveRecord::Base
  validates_formatting_of :first_name, :regex => /[A-Z]/i
end

Development and Contribution

It is very easy to contribute to this gem. Full documentation to do so will be added in the near future.

Have Ideas?

Do you use a particular pattern on a regular basis that isn't here or you would like to contribute? For now, create a new issue in the issue tracker. I would be more than happy to consider adding it to the project.

validates_formatting_of's People

Contributors

sferik avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.