Skip to content

Instantly share code, notes, and snippets.

@tboyko
Created March 23, 2012 18:38
Show Gist options
  • Save tboyko/2173624 to your computer and use it in GitHub Desktop.
Save tboyko/2173624 to your computer and use it in GitHub Desktop.
validates :enrollment_method,
presence: true,
inclusion: {
in: ['email', 'phone_number']
}
validates :email,
presence: true,
format: {
with: /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/,
message: "is not of the correct format"},
if: lambda { |device|
device.enrollment_method == 'email'
}
validates :phone_number,
presence: true,
format: {
with: /^[\D]*1?([\D]*[\d][\D]*){10}[\D]*$/, # 10 digits in string, optionally starts with "1" to make 11.
message: "is not a valid US phone number"},
if: lambda {
|device| device.enrollment_method == 'phone_number' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment