indexOf for custom jquery validation rule
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:
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.
精彩评论