Solved

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

Posted on 2014-04-10
1
438 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…

912 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