The problem with OnObjectModify is that it will perform the above script with each individual character scanned into the field from the barcode.
Thus when the first character is scanned in, nothing happens because length is 1, then the second character appears in the field and the script performs again. Now youhave 2 characters and still nothing happens, but when the fifth digit of that 6 digit barcode is scanned in, you now have 5 characters in the field and it is processed as a 5 digit scan before that fifth digit ever appears in the field.
What I do with scanners is to research their features to learn how to configure them to append a character to the end of the scanned text. I then set it to append either return, tab or enter and set field behavior to recognize that character as the keystroke to exit the field. Now I can set an OnObjectExit trigger to perform the script that processes the scanned data.
Super easy!! Thanks.