Solved

Display a button only when a particular error message is triggered with jquery validate

Posted on 2014-04-10
1
431 Views
Last Modified: 2014-04-21
I have a form using jquery validate that uses ajax to check the email address to see if:
1. it is in a valid format
2. it already exists in the system.

		$('#form_user').validate({
			rules: {
				email_address: {
					required: true,
					email: true,
					remote: {
						url: site_url+"/users/email_address_check",
						type: "post",
						async:false,
						data: {
							email: function() {
								return view_model.user.email_address();
							}
						}
					}
				}
			}
		});

Open in new window


The email_address_check function returns a (JSON encoded) error message when it detects an existing user with the given email address, as per the requirements for using the "remote" method. If the email is simply invalid, then the "email: true" setting shown above causes a different error to be displayed.

I want to display (or enable) a button when an existing user is found with the given email address. How can I detect when the email address has failed for this reason, rather than simply being invalid?
0
Comment
Question by:Terry Woods
1 Comment
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 500 total points
ID: 39993482
Customize your email messages for each case (required, not valid, remote method) like below. Now, since you already knew the messages for required case and not valid format, please use this to detect whether the message is from the remote method or not and display the button as per your requirement.

$('#form_user').validate({
                  rules: {
                        email_address: {
                              required: true,
                              email: true,
                              remote: {
                                    url: site_url+"/users/email_address_check",
                                    type: "post",
                                    async:false,
                                    data: {
                                          email: function() {
                                                return view_model.user.email_address();
                                          }
                                    }
                              }
                        }
                  },
                  messages:
                       {
                         email:
                         {
                            required: "Please enter an email address.",
                            email: "Please enter a valid email address.",
                            remote: jQuery.validator.format("{0} ")
                         }
                       }
            });
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now