views:

30

answers:

1

I received some great help here the other day and hope once again I can get the answer I need as Im pretty stuck right now. I have a form that has a text input (#USA_sub) and two subsequent text input's (#FirstName) and (#LastName) I have a validation rule that checks to see if each value of (#FirstName) and (#LastName) each appear in (#USA_sub). What I have is working except for this: when you enter the correct value in the (#FirstName) input, correct in that it is contained in (#USA_sub) you only have to enter 2 letters in last name for it to validate. If you skip First Name it requires all of the last name as it should.

$.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."
);

$.validator.addMethod(
    "lastSig", 
    function(value, element, params) {
        return $(params).val().indexOf($("#FirstName").val() + ' ' + value) > -1;
}, 
    "Your last name must be contained in your Electronic Signature."
);

and the validation rules:

                                  FirstName: {
                    required: true,
                    minlength: 2,
                    firstSig: "#USA_sub"
                },
                LastName: {
                    required: true,
                    minlength: 2,
                    lastSig: "#USA_sub"
                }

Thank you!

A: 

The reason it's doing that is that you're testing part of the name against the full name. As long as the target string contains consecutive characters matching the test string, there will be a match.

For example:

FirstName = Bob
LastName = Dylan

USA_sub = Bob Dylan

user has typed: Bob Dyl

There's a match because the indexOf() found Bob Dyl within Bob Dylan

Try just doing an == search, as in:

return $(params).val() == ($("#FirstName").val() + ' ' + value);

The reason your FirstName validator works, is that you're concatenating in the space at the end.

So Bo(space) is not found in Bob(space)Dylan. But Bob(space) is found in Bob(space)Dylan


EDIT: New version that uses a regular expression to test for beginning/end of input, and eliminates the cross reference from FirstName to LastName and vice versa.

The previous version didn't work because you were always validating the FirstName and LastName against the FirstName field.

Because of this, when you tab over to the LastName field, which is presumably empty, the validation fails for FirstName, and it doesn't re-validate until you go back to the FirstName.

What I did below was to remove the cross-field referencing, and use a regular expression so that we are able to test for beginning/end of line. So basically what we have is:

FirstName tests for - BeginningOfInput + FirstName + space LastName tests for - space + LastName + EndOfInput

$.validator.addMethod(
    "firstSig", 
    function(value, element, params) {
          // The regular expression represents
          //    beginning of input + value + space
        var regex = new RegExp('^' + value + ' ');
        return regex.test($(params).val());
    }, 
    "Your first name must be contained in your Electronic Signature."
);

$.validator.addMethod(
    "lastSig", 
    function(value, element, params) {
          // The regular expression represents
          //    space + value + end of input
        var regex = new RegExp(' ' + value + '$');
        return regex.test($(params).val());
}, 
    "Your last name must be contained in your Electronic Signature."
);

This way you don't get a validation failure when you've entered a correct FirstName, but haven't yet gotten to the LastName field.

patrick dw
return $(params).val()===(value + ' ' + $("#LastName").val());andreturn $(params).val()===($("#FirstName").val() + ' ' + value);seem to work better. I really appreciate the explanation, now I understand it rather than just doing it!Thanks Patrick!
Dirty Bird Design
@Dirty - You're welcome. :o)
patrick dw
@patrick, youve been a huge help, could you take a look at some unexpected behavior with above? when you enter your first name, you get the error until you enter your last name and go back to first name, delete it and reenter it...
Dirty Bird Design
@Dirty - Sure, I'll take a look, but just wondering, is there some reason why you're validating both the First and Last name for both fields? It would seem as though it would be sufficient to have each field test only its part. Or perhaps test for (beginLine + FirstName + Space) and (Space + LastName + endLine) or something. I'll still take a look, though.
patrick dw
Im open to any suggestions! basically I have to validate that the "#FirstName" is contained in "#USA_sub" and "#LastName" is contained in "USA_sub"http://www.kinetick.com/FOO/purchasestep 4 with the agreements contains #USA_sub and step 5 has the "FirstName" and "LastName" thx man!
Dirty Bird Design
@Dirty - I just updated with a new example and an explanation. Let me know how it works for you.
patrick dw
@Patrick Man, you are awesome! I will definitely have to read on what a "regrex/regular expression" does, that stuff is way over my head right now. It works perfect. thanks a lot Patrick, now I just need to figure out whats wrong with my xml function and Im set. Thanks, this is greatly appreciated!
Dirty Bird Design
@Dirty - Not a problem.
patrick dw