forked from jeremiahishere/format_validators
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added zip code and phone format validators.
- Loading branch information
1 parent
bdd8f78
commit 69183a9
Showing
5 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Custom Validations for Phone Number format | ||
|
||
# e.g. validates :home_phone, :phone_format => true | ||
# e.g. validates :cell_phone, :phone_format => { :allow_blank => true, :if => :method_returns_true } | ||
class PhoneFormatValidator < ActiveModel::EachValidator | ||
def validate_each record, attribute, value | ||
format = /^1?[-\. ]?(\(\d{3}\)?[-\. ]?|\d{3}?[-\. ]?)?\d{3}?[-\. ]?\d{4}$/ | ||
message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.' | ||
record.errors[attribute] << (options[:message] || message ) unless value =~ format | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Custom Validations for Zip Code format | ||
|
||
# e.g. validates :zip, :zipcode_format => true | ||
# e.g. validates :zip, :zipcode_format => { :allow_blank => true, :if => :method_returns_true } | ||
class ZipcodeFormatValidator < ActiveModel::EachValidator | ||
def validate_each record, attribute, value | ||
format = /^\A[\d]{5}(?:[-|\s][\d]{4})?\Z$/ | ||
message = attribute.to_s.humanize + ' doesn\'t match an acceptable format.' | ||
record.errors[attribute] << (options[:message] || message ) unless value =~ format | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
describe PhoneFormatValidator do | ||
describe ".validate_each" do | ||
before(:each) do | ||
@options = {:attributes => {}} | ||
@validator = PhoneFormatValidator.new(@options) | ||
@record = BasicRecord.new(:phone) | ||
end | ||
|
||
it "should validate 7 digit phone number" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "123-4567") | ||
end | ||
|
||
it "should validate 10 digit phone number without area code parentheses" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "321-123-4567") | ||
end | ||
|
||
it "should validate 10 digit phone number with area code parentheses" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "(321) 123-4567") | ||
end | ||
|
||
it "should validate 10 digit phone number with a 1 first" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "1-321-123-4567") | ||
end | ||
|
||
it "should allow dashes between numbers" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "1-321-123-4567") | ||
end | ||
|
||
it "should allow a single space between numbers" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "1 321 123 4567") | ||
end | ||
|
||
it "should allow multiple whitespace characters between numbers" do | ||
@record.errors[:phone].should_not_receive("<<") | ||
@validator.validate_each(@record, :phone, "123 4567") | ||
end | ||
|
||
it "should not validate 5 digit phone number" do | ||
@record.errors[:phone].should_receive("<<") | ||
@validator.validate_each(@record, :phone, "3-4567") | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'spec_helper' | ||
|
||
describe ZipcodeFormatValidator do | ||
describe ".validate_each" do | ||
before(:each) do | ||
@options = {:attributes => {}} | ||
@validator = ZipcodeFormatValidator.new(@options) | ||
@record = BasicRecord.new(:zip_code) | ||
end | ||
|
||
it "should validate 5 digit zip codes" do | ||
@record.errors[:zip_code].should_not_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "12345") | ||
end | ||
|
||
it "should validate 9 digit zip codes with a space"do | ||
@record.errors[:zip_code].should_not_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "12345 6789") | ||
end | ||
|
||
it "should validate 9 digit zip codes with a dash"do | ||
@record.errors[:zip_code].should_not_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "12345-6789") | ||
end | ||
|
||
it "should not validate 9 digit zip codes with nothing between the first 5 and last 4"do | ||
@record.errors[:zip_code].should_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "123456789") | ||
end | ||
|
||
it "should not validate zip codes of other lengths"do | ||
@record.errors[:zip_code].should_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "123456") | ||
end | ||
|
||
it "does not have support for canadian zip codes"do | ||
@record.errors[:zip_code].should_receive("<<") | ||
@validator.validate_each(@record, :zip_code, "K1A OB1") | ||
end | ||
end | ||
end |