Email Validation for specific #college.edu only - ruby-on-rails

hello I am building a rails application for the college community, the idea is that only students with a valid #college.edu email address can sign up for it.
There is a students table with college as its column
I have looked at the rails doc under validation, mostly it tells you how to validate length, presence, emptiness etc.
will this gem help me?
gem "validates_email_format_of", "~> 1.5.3"
I was reading up on email validation and it go into parsers, RFC 2822 and RFC 3696 ? is there a simpler way to go about it like regular expressions?

I'm not familiar with the validates_email_format_of gem, but the following example of the use of validates is documented in http://apidock.com/rails/ActiveModel/Validations/ClassMethods/validates
validates :email, :format => { :with => /\A([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create }

Related

Validations on money-rails gem

I have money-rails gem installed and it's working flawlessly. However, there are no validations on money objects when I create a new model record. If I try to input letters into the money field, the form submission is successful, it just sets the value to 0. How do I get those validations working? I have no code for the actual validation, seeing as money-rails on github states that it has validations included, but I have tried validates_numericality_of to no avail.
EDIT
Yes I have read the docs extensively, and tried the validation option suggested on Github.
I have this example hope it could help you
monetize :price_cents, :numericality => {:greater_than => 0, :less_than => 10}
validates :price, :format => { :with => /\A\d+(?:\.\d{0,2})?\z/ }
just like this one of them (monetize validation) will validate the numericality stuff and the other one will validate the format.

Validate on custom words

I have a field in my form, that should not accept some specific words(www, ftp, smtp, etc). Is there any validator that could make some kind of black listed words, that can not be written to db?
validates :subdomain, :exclusion => { :in => %w(www ftp smtp) }
ref: rails guide
You should create your own black list validator.
The syntax could be
validates :field, :black_list => {:file_path => "/path/to/words_file"}
Your validator will look to each word in the /path/to/words_file file and add errors on your model if the attribute field contains one black listed word.

Validate US Zip Code format with Rails

How do you validate a U.S. zip code using Rails?
I wrote something like this but it doesn't work:
validates_format_of :zip_code,
:with => /^\d{5}(-\d{4})?$/,
:message => "Zip code should be valid"


You can also validate that it is actually a valid zip (not just the format but the zip itself) using:
http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP
Try a valid zip you know, e.g. 02135 vs an invalid one like 09990 to see the difference.
I would consider combining this with:
validates_format_of :zip, :with => /^\d{5}(-\d{4})?$/, :message => "should be in the form 12345 or 12345-1234"
that it's done with validate_format_of, rather than validate_format_of_zip_code as that means it can also be used for phone numbers, etc that also fit a fixed, known, format (.e.g. U.S.).
Perhaps validate format first and give error if invalid, so handle it all within rails with standard flash message.
Then, if valid make the call to that server to validate actual zip itself.
The only downside to great server supplied validations like this is that they increase the dependency on other sites and services. So if the other site/service changes things or is unavailable, etc. there is an issue. This is another reason why doing the simpler validity check first is a great idea.
A full service solution would also check for time-out by the zip code service and if that happens, say 5 seconds and the format is ok probably best to accept value. Maybe set an 'unverified_zip' flag if possible!
This worked for me: (ruby-2.0.0-p247, rails 4.0.0)
validates_format_of :zip_code,
with: /\A\d{5}-\d{4}|\A\d{5}\z/,
message: "should be 12345 or 12345-1234",
allow_blank: true
If you need multi-country support, you can use validates_zipcode gem I released. It currently supports 159 countries zipcode formats and plays nice with Rails 3 & 4.
You can use it like this:
class Address < ActiveRecord::Base
validates_zipcode :zipcode
validates :zipcode, zipcode: true
validates :zipcode, zipcode: { country_code: :ru }
validates :zipcode, zipcode: { country_code_attribute: :my_zipcode }
end
ZIP codes in the US are either 5 digits or 5 digits plus 4 digits for the area code. Try the following:
validates_format_of :zip_code,
:with => %r{\d{5}(-\d{4})?},
:message => "should be 12345 or 12345-1234"
those are both good answers!
another idea is to create you own custom validation , which not only checks that the number of digits is correct, but also checks with a database in the background, that the zip-codes exist..
e.g. these Gems could help:
geokit , check here: Best Zip Code Plugin for Ruby
zip-code-info , http://rubygems.org/gems/zip-code-info
http://zipcodesearch.rubyforge.org/

Rails - How to build a custom validation

I have a Rails 3 form, with data-remote => true.
The form field is handle, like a twitter handle. I want to add validation to ensure it's handle friendly, meaning, no spaces, or non-url friendly characters.
Where to start, do I build a customer validation for this?
Thanks
Looks like you want to use validates_format_of
class Product < ActiveRecord::Base
validates_format_of :handle, :with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"
end
Change the regular expression pattern to match your needs. See the Rails Guides on validation for more information.
Look into validates_each
validates_each :handle do |record,attribute,value|
# your validation code here, and you can record.errors.add().
end
In rails 3 the best practice will be using
validates :handle, :format => {:with => /\A[a-zA-Z]+\z/, :message => "Only letters allowed"}

validating presence of email address (can't be blank)- Ruby on Rails

Pretty new at all this. I have a simple form for users to enter a couple pieces of information and then input their email address and push the submit button. I want to make it mandatory that they have to fill out their email address in order to push the submit button. If they don't fill out their email address they should get an error message on the email box that says the email can't be blank. I know this is super simple but I need exact help on where to put what code. I've been researching this all night and know that part of the code should go in the application_controller and other should go in the html file where the actual text_field:email is.
I'd be grateful if someone could clearly tell me what the necessary steps are for doing this. Thanks!
It should go in your model. Add this:
class Model < ActiveRecord::Base
validates_presence_of :email
end
Check this link for more info: http://guides.rails.info/activerecord_validations_callbacks.html#validates-presence-of
In Rails 2, which I would assume you are using, validations go in the model. Which is located in $Rails_app_directory/app/model/$Classname.rb
In order to add ActiveRecord validations you can use the line
validates_presence_of :email_address
You should also consider using Rails to generate a confirmation field and filtering out ill-formatted email addresses. You could accomplish the former with:
validates_confirmation_of :email_address
with this, all you need to add to your form is a text_field for :email_address_confirmation
and the latter with a regular expression such as:
validates_format_of :email_address, :with => /\A[\w\.%\+\-]+#(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|jobs|museum)\z/i
From snipplr, place in your model
validates_format_of :email,
:with => /^([^#\s]+)#((?:[-a-z0-9]+\.)+[a-z]{2,})$/i,
:message => 'email must be valid'

Resources