Minimum and Maximum Distance
Join the Discussion
In previous tutorials we have looked at checking for the existance or non-existance of specific characters or character combinations. We have also looked at how to check that the characters appear in the required order. It may not be enough though that the characters are present and in the right order, there may also be a minimum or maximum number of other characters that must be between those characters for the entry to be valid. The following function shows how to achieve this:
var pos1 = first == '' ? 0 :
var pos2 = last == '' ? parmField.length - pos1 - 1 :
if (pos1 == -1) return false;
if (pos2 < min || pos2 > max) return false;
return true; }
This function has five arguments. The first is the field we are testing, the second and third are the characters that we are testing and the fourth and fifth are the minimum and maximum number of characters that must appear between them.
The first two lines within the function test for the location of the first character. If an empty string is passed then the start of the field is assumed and we will be testing that the second character passed is an appropriate distance from the start of the string.
The following two lines then perform exactly the same search for the second character except that only the part of the string following the first character is searched. If an empty string is passed here then the end of the field is assumed and we will be testing that the first character passed is an appropriate distance before the end of the string.
If both of these parameters are empty strings then the portion of the string that we are testing the length of is the entire string.
The test will fail if either of the specified characters is not found so the next line returns false if the first character is not in the string. This is then followed by a test that the distance from the first character to the second is between the minimum and maximums specified. If the second character is not found then the distance will be -1 which will be less than the minimum and so a separate test for this is not required.
This function is a direct replacement for the setOrder function we used to check character order and you can achieve the same functionality as in that function by passing 0 in the fourth parameter and fld.length in the fifth parameter. Let's look at the email email address validation again and with our new function test to see that there are at least two characters between the at and period symbols (and at least two characters before the @ and after the .):
fld = stripBlanks(fld);
if (fld == '') return false; // test mandatory
return false; // test for only one @
if (adjacent(fld,'@.') || adjacent(fld,'.@')
return false; // test that .@ @. and .. do not occur
if (!setDistance(fld,'@','.',2,fld.length) return false;
if (!setDistance(fld,'','@',2,fld.length) return false;
if (!setDistance(fld,'.','',2,fld.length) return false;
// other validations for this field to be added here