Solved

jquery validation not equal

Posted on 2012-03-24
6
3,204 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Suggested Solutions

Title # Comments Views Activity
JavaScript Loan Calculator Error 3 35
Video Tutorial help 2 40
PHP Web Development 6 29
Compute age Html 2 27
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

726 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