views:

43

answers:

3

Ive been struggling with this all day, been close a couple times but nothing seems to work exactly.

I have a text input <input type="text" name="USA_sub" id="USA_sub" /> and after that an input <input type="text" name="FirstName" id="FirstName" /> I need to make sure (create a validation rule) that states "#FirstName" 's value must be contained in "#USA_sub" 's value.

I keep getting the error "$(sig).contains is not a function" with this:

$.validator.addMethod("FNESig", function (value, element) {
    var firstname = $("#FirstName").val();
    var sig = $("#USA_sub").val();

    if ($(sig).contains(firstname)) {
        return true;
    } else return false;
}, "Your First Name must be in your Electronic Signature.");

thx Kane

+1  A: 

contains method in jQuery can only be applied to DOM elements. You may want to use indexOf javascript method instead. It will search for an occurrence of the specified value in a string

$.validator.addMethod("FNESig", function (value, element) {
    var firstname = $("#FirstName").val();
    var sig = $("#USA_sub").val();

    // means: does sig contains firstname?
    return (sig.indexOf(firstname) !== -1);

}, "Your First Name must be in your Electronic Signature.");
galambalazs
galambalazs thank you! this is perfect
Dirty Bird Design
@Dirty Bird - This *is* a great answer. Should be since it was edited to borrow from **Nick Craver's** answer. http://stackoverflow.com/posts/3190198/revisions
patrick dw
Man didn't mean anyone's feelings to get hurt, I didn't read Nick's completely. Thanks Nick, all help is appreciated.
Dirty Bird Design
I wasn't borrowed anything lol. You can see I do a lot of revisions, because I'm testing and tweaking in jsbin. Someone has no life over SO, and making conspiracy theories... :) have a good night sleep, dude ;)
galambalazs
@galambalazs tell em
Dirty Bird Design
@patrick who cares points btw? The OP solved the problem? Yes. I'm happy. You got accepted. Fine by me. Who cares.... lool. This site is about helping others btw, if you haven't noticed before...
galambalazs
No theories at work here. It is low class to change your answer to match someone else's.
patrick dw
@Dirty Bird - Tell em? So why did you change your accepted answer? Doesn't make much sense.
patrick dw
@patrick What else do you want? Nick's answer is accepted, lol. I said I don't care. There is nothing left here to talk about. Get a life man...
galambalazs
galambalazs - I don't want a thing. Dirty decided to change his accepted answer. I merely pointed out that you changed your answer to match Nick's. I'm sorry that you're having trouble with his decision.
patrick dw
Dirty Bird Design
@Dirty - With all due respect, I have *no* idea why you think I'm pissed. All I did was point out that he changed his answer to match the content from another (not mine), which is in poor taste. For some reason you made a comment about hurt feelings and said *tell em* like there was some sort of battle. I have no idea why. If I knew people would get so emotional over it, I would have refrained. SO is a great place. I try to help people here, but have received much more than I have given. I hope you receive as much benefit as I have. Best of luck to you.
patrick dw
@galambalazs - My apologies. I didn't mean to start anything this big over your answer. Best of luck to you, as well.
patrick dw
My attempt at sarcasm. Get em. epic fail. :) thanks to all, but I had to use a different solution anyway. Both galambalazs and Nick's would validate the last name in the first name input against the variable. Here is what I ended up using. $.validator.addMethod( "firstSig", function(value, element, params) { return $(params).val().indexOf(value + ' ' + $("#LastName").val()) > -1; }, "Your first name must be contained in your Electronic Signature." );
Dirty Bird Design
+3  A: 

Try this:

if (sig.indexOf(firstname) >= 0) {
    return true;
} else return false;
patrick dw
And this was down voted, why?
patrick dw
Why was this downvoted? It's correct, he has `>= 0`, not just `> 0`. +1
Nick Craver
Thanks Nick. :o)
patrick dw
+3  A: 

Instead of this ($().contains() isn't a method...and you have a string, not a selector for a jQuery object, no need to wrap anything):

if ($(sig).contains(firstname)) {
    return true;
    } else return false;

You can just use string.indexOf(), like this:

return sig.indexOf(firstname) != -1; //-1 would mean it wasn't found

As a side note, since you're returning the same result as the condition, just return it, in other words this:

if(someCondition) {
  return true;
} else {
  return false;
}

Can just be:

return someCondition;
Nick Craver
+1 Nice way to eliminate the unnecessary `if()`.
patrick dw