I need create a method the custom validation called findUpperCaseLetter or similar
I have a attribute called password that belongs to the model called User
My method is somewhat similar to the following. The password attribute must have at least a uppercase letter
def findUpperCaseLetter(username)
username.each_char do |character|
return true if character=~/[[:upper:]]/
end
return false
end
I want add this validation to the User model
class User < ActiveRecord::Base
attr_accessible :user_name,:email, :password
validates_presence_of :user_name,:email, :password
validates_uniqueness_of :user_name,:email
????
end
I have already the following regular expreession
validates_format_of :password, :with => /^[a-z0-9_-]{6,30}$/i,
message: "The password format is invalid"
How I modify it for add the following
The password must have at least one uppercase letter
You don't need custom validation to do this, you can use regex to validate the format of password. Add this to your model
In Rails, you can do this by using
format
validator, I have addedallow_blank: true
to make sure when the field is empty it only throwsCan't be blank
error message and not format validator error message.The work of this regular expression is to allow the password to be saved in the database only if it contains at least one capital letter. I have created a permalink on rubular, click here http://rubular.com/r/mOpUwmELjD
Hope this helps!