[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 489
  • Last Modified:

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

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
Terry Woods
Asked:
Terry Woods
1 Solution
 
jayakrishnabhCommented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now