Reputation: 6071
I am using the JQuery Validation plugin to validate a sign up form on my website.
I want to use a remote call to see if an email exists.
However, I am having difficulty.
The form keeps returning the message "Please enter a valid email address"
I have been following this tutorial: http://sleekd.com/tutorials/jquery-validation-in-ruby-on-rails/
Below is my code so far:
JS File
$(document).ready(function() {
$("#new_account").validate({
rules: {
"account[name]": {required:true},
"account[email]": {required:true, email:true, remote:"/check_email"},
"account[password]": {required:true},
"account[gender]": {required:true}
}
});
});
Route
match 'check_email/:email' => 'accounts#checkEmail', :as => :check_email
Controller
def checkEmail
account = Account.getAccountByEmail(params[:email])
respond_to do |format|
format.json {render :json => !account}
end
end
Appreciate any help.
Thank you,
Brian
Upvotes: 1
Views: 1517
Reputation: 3009
Not sure it this exactly helps you(sorry for that), but surely worth a shot. However, do proceed first as specified in the solution by Vincent. The value of account might be the issue.
Have you checked this gem for client side validations. Its at https://github.com/bcardarella/client_side_validations Its easy to use and you dont even need to write your own js.
There's a railscast on it too http://railscasts.com/episodes/263-client-side-validations
Upvotes: 1
Reputation: 475
Check the output of getAccountByEmail
when you give it a known address and when you give it a new email address via the rails console
. For it to always return true, your variable account
has to be false
or nil
. That's where I would start.
Upvotes: 1