Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
## 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

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