?
Solved

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

Posted on 2014-04-10
1
Medium Priority
?
476 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

801 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