Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

JQuery addMethod validation

Posted on 2014-04-06
8
Medium Priority
?
501 Views
Last Modified: 2014-04-19
Hi, i am trying to add a new addMethod in jquery validation Plugin so validate the two textfields containing comma separated values must be same

like if textfield1 has 6 values, textfield2 must have 6 values too.

Here is what i have tried so far, but it seems wrong

$.validator.addMethod("sametosame", 
function(value,element) {
    var thing1 = $(element).val().length;
    var thing2 = $(element).val().length; 
    if (thing1 == thing2) {
        return true;
    }
    else {
        return false;
    } 
},'Contents must have same length');

Open in new window

0
Comment
[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
8 Comments
 
LVL 75

Assisted Solution

by:Michel Plungjan
Michel Plungjan earned 800 total points
ID: 39982464
You pass one element. You need to get the other elements too.

Please note that this test will be irritating if it is executed on each field as it is filled in.

Try (not tested)

$.validator.addMethod("sametosame", 
  function(value,element) {
    var thisID = element.id;
    var thisPrefix = thisID.replace(/\d*$/, ""); // get "textfield"
    var lengths = $("input[name^='thisPrefix']").map(function() { // get all textfield*'s length
      return this.value.length;
     }).get().join(); // into an array
    // find the smallest and largest length
    var min = Math.min.apply(null, lengths),max = Math.max.apply(null, lengths);
    return (min === max);
},'Contents must have same length');

Open in new window

0
 
LVL 16

Author Comment

by:Gurpreet Singh Randhawa
ID: 39982524
Thanks @mplungjan

I will test it and let you know of the same
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39982609
Typo

 var lengths = $("input[name^='thisPrefix']")

should be

 var lengths = $("input[name^='"+thisPrefix+"']")

assuming the fields you test really do have

xxx1, xxx2, xxx3 as pattern
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 39982757
instead of splitting and joining the strings, which in theory is expensive, you could just count the number of spaces


var numSpaces = function(text) { 
   var result = text.match(/\s+/g).length;
   return result ? result : 0;
}

Open in new window



you may want to  change the expression to not count begging of line or end of line spaces.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39982773
I am not splitting strings.

But I think I have a join() that should not be there


}).get(); // into an array
0
 
LVL 43

Accepted Solution

by:
Rob earned 1200 total points
ID: 39984726
Correct me if I'm wrong but aren't you just wanting to know if both textfields have the same number of values, rather than the same length?

eg.

If textfield1 has 44,2,6,1,111,8 (6 values) and textfield2 has 1,2,3,4,5,6 (also 6 values) then the validation is true?

Here is what I mean:
http://jsbin.com/noqoc/1/edit?js,output

$(document).ready(function() {

  $.validator.addMethod("sametosame", function(value,element) {
    var tmp = value.split(",").length;
    var rtn = true;
    $(".same").each(function(index) {
      console.log($(this).val().split(',').length);
      rtn = rtn && ($(this).val().split(',').length === tmp);
    });
    console.log(rtn);
    return rtn;
  },'Contents must have same length');

  $.validator.addClassRules("same", { sametosame: true });
  $("#test").validate();


});

Open in new window

0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 39985434
Hmm, my validation tests that the lenght of what has been typed is the same.

I agree we need elaboration on what exactly you are testing - example data would be useful
0
 
LVL 16

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 40010335
Thanks Guys
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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

647 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