Solved

jquery validation not equal

Posted on 2012-03-24
6
2,973 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
  • 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now