Solved

jquery validation not equal

Posted on 2012-03-24
6
3,044 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

777 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