Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add phone format validator

  • Loading branch information...
commit 5f2fa242b8ca38827876ba78c97b8ce056596ebb 1 parent d2e2c28
Kris Windham authored
View
4 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- ActiveValidators (1.0.0)
+ activevalidators (1.0.0)
activemodel (>= 3.0.0)
activerecord (>= 3.0.0)
mail
@@ -46,9 +46,9 @@ PLATFORMS
ruby
DEPENDENCIES
- ActiveValidators!
activemodel (>= 3.0.0)
activerecord (>= 3.0.0)
+ activevalidators!
bundler
mail
rspec
View
2  README.md
@@ -22,7 +22,7 @@ Exhaustive list of supported validators:
* `email` : checks the email based on the `mail` gem
* `url` : checks the url based on a regular expression
-
+* `phone` : checks the phone number based on a regular expression
Todo
----
View
11 lib/active_model/validations/phone_validator.rb
@@ -0,0 +1,11 @@
+module ActiveModel
+ module Validations
+ class PhoneValidator < EachValidator
+ def validate_each(record, attribute, value)
+ unless value =~ /^\d{3}-\d{3}-\d{4}|\d{3}\.\d{3}\.\d{4}|\d{10}|\d{3}\s\d{3}\s\d{4}|\(\d{3}\)\s\d{3}-\d{4}$/i
+ record.errors[attribute] << (options[:message] || "is invalid")
+ end
+ end
+ end
+ end
+end
View
1  lib/active_validators.rb
@@ -10,5 +10,6 @@ module Validations
autoload :EmailValidator
autoload :UrlValidator
autoload :RespondToValidator
+ autoload :PhoneValidator
end
end
View
7 spec/models/phone_validator_model.rb
@@ -0,0 +1,7 @@
+module Models
+ class PhoneValidatorModel
+ include ActiveModel::Validations
+ attr_accessor :phone
+ validates :phone, :phone => true
+ end
+end
View
47 spec/specs/phone_spec.rb
@@ -0,0 +1,47 @@
+require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
+
+describe "Phone Validation" do
+
+ it 'should validate format of phone with ###-###-####' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '999-999-9999'
+ model.valid?.should == true
+ model.should have(0).errors
+ end
+
+ it 'should validate format of phone with ##########' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '9999999999'
+ model.valid?.should == true
+ model.should have(0).errors
+ end
+
+ it 'should validate format of phone with ###.###.####' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '999.999.9999'
+ model.valid?.should == true
+ model.should have(0).errors
+ end
+
+ it 'should validate format of phone with ### ### ####' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '999 999 9999'
+ model.valid?.should == true
+ model.should have(0).errors
+ end
+
+ it 'should validate format of phone with (###) ###-####' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '(999) 999-9999'
+ model.valid?.should == true
+ model.should have(0).errors
+ end
+
+ it 'should validate format of phone' do
+ model = Models::PhoneValidatorModel.new
+ model.phone = '999'
+ model.valid?.should == false
+ model.should have(1).errors
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.