Created
January 21, 2009 03:43
-
-
Save base10/49841 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
## Copyright 2009 Rex Luther Corporation. | |
## Licensed under the MIT License | |
## http://www.opensource.org/licenses/mit-license.php | |
def valid_phone | |
return true if phone.blank? | |
phone_formats = [ | |
/^\(\d\d\d\) \d\d\d-\d\d\d\d/, | |
/^\d\d\d\.\d\d\d\.\d\d\d\d/, | |
/^\d\d\d\-\d\d\d\-\d\d\d\d/ | |
] | |
valid = false | |
phone_formats.each do |format| | |
if phone.match( format ) | |
valid = true | |
end | |
end | |
unless valid | |
errors.add("Phone format isn't recognized") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No need to loop and have separate regexes. You can use the
|
operator to permit multiple formatshttp://refiddle.com/gv3