Solved

jquery validation not equal

Posted on 2012-03-24
6
3,310 Views
Last Modified: 2012-03-30
how can I validate two fields must not equal each other with jquery?
I know this code works to validate that they are equal.  I want to do the opposite.

$("#account_info").validate( {
		 
	  rules: {
       profile_2: { equalTo: "#profile_1"}
	     },
		 
	  		 
	     });

Open in new window

0
Comment
Question by:Luey
[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
  • 3
  • 2
6 Comments
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 500 total points
ID: 37762728
http://stackoverflow.com/questions/3571347/how-to-add-a-not-equal-to-rule-in-jquery-validation

jQuery.validator.addMethod("notEqualTo", function(value, element, param) {
  return this.optional(element) || value != param;
}, "Please specify a different (non-default) value");

.
.
.

   rules: {
       profile_2: { notEqualTo: "#profile_1"}
	     },
		 
	  		 
	     });

Open in new window

0
 
LVL 18

Expert Comment

by:nap0leon
ID: 37762753
Conveniently, change "equalTo" to "notEqualTo"
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37762899
It is already implemented?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Luey
ID: 37763638
Tried notEqualTo and it did not work.  

Also tried to add the method and cannot get that to work either.  I have added other methods and they work but cannot see what is wrong with this one.
here is all the code that I am using.

//validation addMethods for my account here
        //custom validation rule - text only
        $.validator.addMethod("textOnly", function(value, element) {
        return !/[0-9]*/.test(value);
        }, "Alpha Characters Only.");

       $.validator.addMethod("notEqualTo", function(value, element, param) {
        return this.optional(element) || value != param;
        }, "Please specify a different (non-default) value");
/*--------------------------------------------------------------------------------------*/
$("#account_info").validate( {
							
	  errorPlacement: function (error, element) {        
	   $('#'+element.attr("id")+'_label').append(error); 		 
         },
		 
		 
	  rules: {
       profile_2: { notEqualTo: "#profile_1"}
	     },
		 
	  messages: {
	   county_ansi: "Setting your location is required. Please enter a city or zip code to search.",
		 },
		 
	  submitHandler: function(form) {
       $("#loading_box").show();
       $("#button_box").hide();
	   form.submit();    
        }
		 
	     });

Open in new window

0
 

Accepted Solution

by:
Luey earned 0 total points
ID: 37763649
Got it

$.validator.addMethod("notEqualTo", function(value, element, param) {
        return this.optional(element) || value != $(param).val();
        }, "Please specify a different (non-default) value");

Open in new window

0
 

Author Closing Comment

by:Luey
ID: 37786156
This worked as intended
0

Featured Post

Industry Leaders: 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

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

717 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