Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
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…

770 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