These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Detecting a character in an item from an array

StevenD
Registered: Oct 6 2006
Posts: 368
Answered

Is it possible to see if a certain character exists in an item from an array?
 
//var myAray = new Array("1","2-3","4","5-8");
 
I want to see if there is a "-" in any of the items of the above array. If there is a "-" in an item from the array then do something. If there isn't a "-" in item from the array then do something else.

StevenD

My Product Information:
Acrobat Pro Extended 9.4.3, Windows
George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1875
Accepted Answer
If you want to test the entire array to see if it includes any items with a dash, this should do it:
  1. if (myAray.toString().match(/-/)) {
  2. // Do something
  3. } else {
  4. // Do something else
  5. }
StevenD
Registered: Oct 6 2006
Posts: 368
While I was waiting for a reply I came across a string.indexOf() method and was trying to figure out how it worked but your solution seems to be easier to use. I tried it out and it works very well. Thanks for the sample.

Match must have something to do with regular expressions.

StevenD

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
An example of how a VIN can be verified;

function ValidateVIN(cVIN) {
// ------- value of letter:0, 1, 2, 3, 4, 5, 6, 7, 8, 9
var aVINLetters = new Array("", "AJ|", "BKS", "CLT", "DMU", "ENV", "FOW", "GPX", "HQY", "IRZ");

// character position: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11, 12,13, 14, 15, 16, 17
// ------------weight: 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2
var aVinWeight = new Array(8, 7, 6, 5,4, 3, 2,10, 0, 9,8, 7, 6,5, 4, 3,2);
var bReturn = (cVIN.length == 17);
if(bReturn) {
var controlchar;
var nSum = 0; // sum of weighted values
var nVal = 0; // value for individual position
var nNextChar = 0; // next character to test
cVIN = cVIN.toUpperCase(); // force VIN to upper case
for (i=0 ; i < cVIN.length; i++) {
nNextChar = cVIN.charAt(i);
for (j in aVINLetters) {
// change letters to numbers
if(aVINLetters[j].indexOf(nNextChar) != -1) {
nNextChar = j;
} // end if aVINLetters
} // end for j in aVINLetters
val = parseInt(nNextChar);
nSum += Number(nVal) * aVinWeight[i];
}
controlchar = Number(nSum) % 11;
if(controlchar == 10) {
controlchar = "X"; // change 10 to "X"
}
bReturn = (controlchar == cVIN.charAt(8));
}
return bReturn;
} // end ValidateVIN function


One needs to convert some Alphabetical characters to a number. The alphabetical character is placed in the n element of the array that corresponds to that letters numeric value. The the array is tested with the '.indexOf()' method to get the number for that alphabetical character.

George Kaiser