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"
}
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:
There’s a match because the
indexOf()foundBob DylwithinBob DylanTry just doing an
==search, as in:The reason your
FirstNamevalidator works, is that you’re concatenating in the space at the end.So
Bo(space)is not found inBob(space)Dylan. ButBob(space)is found inBob(space)DylanEDIT: 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
FirstNameandLastNameagainst theFirstNamefield.Because of this, when you tab over to the
LastNamefield, which is presumably empty, the validation fails forFirstName, and it doesn’t re-validate until you go back to theFirstName.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:
This way you don’t get a validation failure when you’ve entered a correct
FirstName, but haven’t yet gotten to theLastNamefield.