Solved

jquery validation not equal

Posted on 2012-03-24
6
2,889 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
Comment Utility
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
Comment Utility
Conveniently, change "equalTo" to "notEqualTo"
0
 
LVL 75

Expert Comment

by:Michel Plungjan
Comment Utility
It is already implemented?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Luey
Comment Utility
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
Comment Utility
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
Comment Utility
This worked as intended
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

772 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

11 Experts available now in Live!

Get 1:1 Help Now