PageRenderTime 96ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/extra_validations.rb

https://github.com/pixeltrix/extra_validations
Ruby | 26 lines | 19 code | 7 blank | 0 comment | 0 complexity | 5b367f5e8a2de9a4b8eea1e91fae02c5 MD5 | raw file
  1. module ExtraValidations
  2. EMAIL_ADDRESS_REGEX = /\A(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}\Z/i
  3. WEB_ADDRESS_REGEX = /\Ahttp(s)?:\/\/([-a-z0-9]+)(\.[-a-z0-9]+)*?\.([a-z]{2,5}){1}\/?([-a-zA-Z0-9?$%&+=\/]{0,255})\Z/
  4. UK_POSTCODE_REGEX = /\A[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}\Z/
  5. PHONE_NUMBER_REGEX = /\A((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{3,4})(( x| ext)\d{1,5}){0,1}\Z/i
  6. def validates_as_email_address(*args)
  7. args << {:with => EMAIL_ADDRESS_REGEX}.update(args.extract_options!); validates_format_of(*args)
  8. end
  9. def validates_as_web_address(*args)
  10. args << {:with => WEB_ADDRESS_REGEX}.update(args.extract_options!); validates_format_of(*args)
  11. end
  12. def validates_as_uk_postcode(*args)
  13. args << {:with => UK_POSTCODE_REGEX}.update(args.extract_options!); validates_format_of(*args)
  14. end
  15. def validates_as_phone_number(*args)
  16. args << {:with => PHONE_NUMBER_REGEX}.update(args.extract_options!); validates_format_of(*args)
  17. end
  18. end
  19. ActiveRecord::Base.send :extend, ExtraValidations