Skip to content

Instantly share code, notes, and snippets.

@base10
Created January 21, 2009 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save base10/49841 to your computer and use it in GitHub Desktop.
Save base10/49841 to your computer and use it in GitHub Desktop.
## 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
@phallguy
Copy link

No need to loop and have separate regexes. You can use the | operator to permit multiple formats

/^\(\d{3}\)\s?\d{3}-\d{4}|((\d{3}\.){2}\d{4})|((\d{3}\-){2}\d{4})$/g

http://refiddle.com/gv3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment