January 31, 2007
How to use an auto_complete to validate passwords
As a follow up to my previous article about using auto_completes to ensure unique filenames, I’ve put together a bit of code that does essentially the same thing except that it checks a password field to ensure that it meets certain criteria.
-
# view file
-
<%= text_field_with_auto_complete :user, :password %><div id=login_error></div>
-
-
# model file
-
-
def User < ActiveRecord::Base
-
validates_length_of :password, :in=>3..20
-
end
-
-
# controller file
-
-
def auto_complete_for_user_password
-
@user = User.new(params[:user])
-
@user.valid? # ensures the errors array is populated
-
password_errors = @user.errors.on(‘password’)
-
render :update do |page|
-
page.replace_html ‘login_error’, (password_errors.empty? ? "" : password_errors)
-
end
-
end
This will indicate when a password does not meet the validation criteria defined in the model. You can add custom validations to check the strength of the password itself.
Filed by Kevin Olbrich at 9:04 pm under AJAX, Forms, Ruby on Rails, User Interface, auto_complete
No Comments
1 Comment