Solved

Need to alter a javascript onKeyDown event to handle a different input from handheld scanner.

Posted on 2008-06-17
40
339 Views
Last Modified: 2008-06-18
Hello,

I have an application which allows for input from a handheld scanner.  The scanner was initially only using CODABAR codes, but now must accomodate both CODABAR and ISBT128.  So I am in a delima.  For example, I have the following select box on one page:

<select name="BloodType" tabindex="4" onFocus="this.selChar='';" onKeyDown="bar_bloodtype(event)" >
          <option value=""></option>
          <option value="A+">A +</option>
          <option value="A-">A neg</option>
          <option value="B+">B +</option>
          <option value="B-">B neg</option>
          <option value="AB+">AB +</option>
          <option value="AB-">AB neg</option>
          <option value="O+">O +</option>
          <option value="O-">O neg</option>
</select>

The function that is called is as follows:
function bar_bloodtype(e){
      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var keyCode = (e.which)?e.which:e.keyCode;
  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;
  var keyChar = String.fromCharCode(keyCode);
  var selObj = (e.target)?e.target: e.srcElement;
  selObj.selChar += keyChar;
  if(alertTab[selObj.selChar]){
    alert(alertTab[selObj.selChar]);
    return true;
  }
  if(scanTab[selObj.selChar]){
    selObj.value=scanTab[selObj.selChar];
  }
}

Works great in CODABAR when the code for AB+ is scanned (840).  However, when that same code is scanned in ISBT128 format it comes through as:  =%8400.  So what I would like is for the javascript to be altered to look at the first 2 characters.  If it doesn't find "=%" then it should function as it does currently.  If it does find "=%" then it should basically trim off the first 2 characters and the last 1, and then function as it would in CODABAR.  Can anyone help?
0
Comment
Question by:electricd7
  • 24
  • 16
40 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21805934
try:
function bar_bloodtype(e){
      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var keyCode = (e.which)?e.which:e.keyCode;
  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;
  var keyChar = String.fromCharCode(keyCode);
  var selObj = (e.target)?e.target: e.srcElement;
  selObj.selChar += keyChar;
  
  if( selObj.selChar.charAt(0)=="%")
  {
  selObj.selChar = selObj.selChar.substring(2,selObj.selChar.length-1);
  }
  if(alertTab[selObj.selChar]){
    alert(alertTab[selObj.selChar]);
    return true;
  }
  if(scanTab[selObj.selChar]){
    selObj.value=scanTab[selObj.selChar];
  }
}

Open in new window

0
 

Author Comment

by:electricd7
ID: 21806176
That left the select box at BLANK.  Technically I should be able to just add =% in front of the code and it should see that, but for some reason it doesn't recognize the % sign.  Example:

function bar_bloodtype(e){
      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-",
      "=%62":"A+",
      "=%06":"A-",
      "=%73":"B+",
      "=%17":"B-",
      "=%84":"AB+",
      "=%28":"AB-",
      "=%51":"O+",
      "=%95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var keyCode = (e.which)?e.which:e.keyCode;
  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;
  var keyChar = String.fromCharCode(keyCode);
  var selObj = (e.target)?e.target: e.srcElement;
  selObj.selChar += keyChar;
  if(alertTab[selObj.selChar]){
    alert(alertTab[selObj.selChar]);
    return true;
  }
  if(scanTab[selObj.selChar]){
    selObj.value=scanTab[selObj.selChar];
  }
}

Then it should check to see if the first 2 are =% then the code, but it doesnt work.  If I change it to "==##" it works, but that's not the characters the scaner sends :(.  Is there some problem with the % and javascript?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806227
I just noticed that the first character is =, not %. I used % instead. From what I posted earlier change:
if( selObj.selChar.charAt(0)=="%")

to:
if( selObj.selChar.charAt(0)=="=")
0
 

Author Comment

by:electricd7
ID: 21806254
Yea I caught that.  When I did that, it worked fine for codabar (ie, passed 840), but left the select field blank for ISBT (ie, passed =%840).
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806279
Try changing:
selObj.selChar = selObj.selChar.substring(2,selObj.selChar.length-1);

to:
selObj.selChar = selObj.selChar.substring(2,selObj.selChar.length);
0
 

Author Comment

by:electricd7
ID: 21806290
Again, your function works fine if I pass it ==8400, but doesn't work if I pass it =%8400.  Its like the % sign confuses the javascript in any case.  Not sure why?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806476
Try this:
function bar_bloodtype(e){

      var scanTab = {

      "62":"A+",

      "06":"A-",

      "73":"B+",

      "17":"B-",

      "84":"AB+",

      "28":"AB-",

      "51":"O+",

      "95":"O-"

      };

      

      var alertTab = {

      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",

      "40":"Auto unit.",

      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."

      }

  var keyCode = (e.which)?e.which:e.keyCode;

  if(keyCode>95&&keyCode<106) keyCode-=48;

  if(keyCode<48||keyCode>57) return true;

  var keyChar = String.fromCharCode(keyCode);

  var selObj = (e.target)?e.target: e.srcElement;

  selObj.selChar += keyChar;

  if( selObj.selChar.match(/(\d+)/) )

  {

  	selObj.selChar = RegExp.$1;

	alert( "detected" + selObj);

  }

  if(alertTab[selObj.selChar]){

    alert(alertTab[selObj.selChar]);

    return true;

  }

  if(scanTab[selObj.selChar]){

    selObj.value=scanTab[selObj.selChar];

  }

}

Open in new window

0
 

Author Comment

by:electricd7
ID: 21806497
Both labels resulted in Alert detected[object].
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806572
>>detected[object].
Oops. should have been:
alert( "detected" + RegExp.$1);
OR
alert( "detected" + selObj.selChar);

It should reveal only the numeric portion of selObj.selChar
0
 

Author Comment

by:electricd7
ID: 21806612
OK, now both come back Detected5, one should be 510 and the other should have been =%8400.  So I would expect one to say detected 510 and the other detected8400.  Neither one set the value of my select box, though.
0
 

Author Comment

by:electricd7
ID: 21806639
You can try this yourself, btw, without a scanner by simply typing the characters into the select field I pasted above.  The scanner is nothing more than a keyboard emulater that sends the keystrokes.  I get an identical reaction whether I scan the label, or simply type in the value of the label.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806728
If I am reading this correctly this:
var selObj = (e.target)?e.target: e.srcElement;

sets selObj to the <SELECT>. So what does:
selObj.selChar += ...

supposed to be. There is not selChar attribute on a <select>. Did you mean
selObj.value +=  keyChar;

also, examine the value of keyChar.
0
 

Author Comment

by:electricd7
ID: 21806847
Well I would have to really dig.  Its been 4 years since I put this together, and to be honest, it was bits and pieces from a lot of other work.  It works flawlessly on CODABAR, I just thought I could simply check for the first part of the ISBT identifier code (=% in this case) and basically ignore it and keep the function the same.  It looks like this is not the case?  Do you know of any reason why the "%" would throw the whole thing off?  I mean If I pass ==8400 to the function after adding "==84" to scanTab array, it works perfect, but the same can't be said for adding "=%84".  Maybe it has something to do with the function picking up the SHIFT press??
0
 
LVL 82

Expert Comment

by:hielo
ID: 21806893
>> Do you know of any reason why the "%" would throw the whole thing off?
I've never dealt with a scanner input so I don't  know how the data is encoded.

>>I mean If I pass ==8400 ..
And that is even more confusing to me since on your function you are not passing a value. You are passing an event. So I don't know what you mean by:
If I pass ==8400 to the function after adding "==84" to
0
 

Author Comment

by:electricd7
ID: 21806938
I  mean if it TAB to the SELECT box and it is in focus (ie, turns blue), then I type ==8400, the select box changes to whatever character value I set for it in scanTab.  So I added:

"==84":"AB+"

To scanTab in bar_bloodtype, then type in ==8400 while focused on the select field in the form, and the select box changes to AB+.  The same thing happens if I type in only 84.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21807106
can't you just use onchange instead of onkeydown and pass the actual value of the selected option. Then you will not have to mess around with the the event object.
onchange="bar_bloodtype(this.value)"
0
 

Author Comment

by:electricd7
ID: 21807172
But the select box doesn't change because the value I am keying in doesn't equal any of the values in the select box.  The onkeydown event actually causes the select box to change.  Make sense?  The values of the select box look like this:

          <option value=""></option>
          <option value="A+">A +</option>
          <option value="A-">A neg</option>
          <option value="B+">B +</option>
          <option value="B-">B neg</option>
          <option value="AB+">AB +</option>
          <option value="AB-">AB neg</option>
          <option value="O+">O +</option>
          <option value="O-">O neg</option>

But I am not entering in "O-", I am entering "95" which causes the select box to select the O- value.  Does that make any sense?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21807299
>>Does that make any sense?
Yes, except for one thing. It sounds like as you type, the function is triggered, so what's the relation between YOU typing and the scanner?

On another note, since you are entering data manually onevery keypress the function is triggered and these are executed every time:
var selObj = (e.target)?e.target: e.srcElement;
selObj.selChar += keyChar;

so if you were to enter 95, the first time around you would see the 9, but once you press the 5, you loose the previous value - you loose the 9 because on the other keypress you would have:
var selObj = (e.target)?e.target: e.srcElement;

What's even worse, selChar is NOT a standard attribute for a <select>, so the first time around I would expect this to give you problems:
selObj.selChar += keyChar;

because that is equivalent to:
selObj.selChar = selObj.selChar + keyChar

but initially selObj.selChar  should be undefined! Perhaps you should be keeping track of the values in a hidden field OR on a global variable.
0
 

Author Comment

by:electricd7
ID: 21807341
Well Im down for whatever.  The scanner is essentially just a keyboard wedge, so when when you scan a code, it "types" that code into whatever app is open.  It still acts just like if I were hand-keying, only much faster.  What I don't understand is that this has worked for years without a problem, so when I type 9 then a 5, the function DOES remember the 9 somehow and it adds the 5, then looks up in the array and sees that 95 should be O-, and sets the field.  I don't have a problem re-writing the whole function, but it seems like the only thing keeping this function from working is that it doesn't deal with the % symbol correctly for some reason.
0
 

Author Comment

by:electricd7
ID: 21807400
Also, onFocus for the select field I am setting this.selChar='', so basically I am just adding each keystroke to that variable.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21807611
You said:
>>Then it should check to see if the first 2 are =% then the code, but it doesnt work.  If I change it to "==##" it works

And previously you said:
However, when that same code is scanned in ISBT128 format it comes through as:  =%8400.
It sounds like the issue is not the %, but the encoding. I don't know what type of encoding ISBT128 does, but even though YOU see %, the computer sees something else, in this case another =.
0
 

Author Comment

by:electricd7
ID: 21807699
Sorry, I meant if I hand key in ==84, no way to change the scanner to do that. Also I can confirm that the scanner is sending the literal text =%8400. No encoding. Just have to trust me on this one. :)
0
 
LVL 82

Expert Comment

by:hielo
ID: 21807849
Originally I suggested:
if( selObj.selChar.charAt(0)=="=")

Out of curiosity, what does this report:
alert( selObj.selChar.charAt(1) )
0
 

Author Comment

by:electricd7
ID: 21807898
Ill give it a shot as soon as I get to a computer. Answering this on the phone. Thanks.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21808066
Also, since you are using the += operator, check to see what this shows:

alert( selObj.selChar.charAt(1) + " " + selObj.selChar )
0
 

Author Comment

by:electricd7
ID: 21808168
OK here is my function now:

      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var keyCode = (e.which)?e.which:e.keyCode;
  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;
  var keyChar = String.fromCharCode(keyCode);
  var selObj = (e.target)?e.target: e.srcElement;
  selObj.selChar += keyChar;
 
  if(selObj.selChar.charAt(0)=="=")
  {
  alert( selObj.selChar.charAt(1) )
  }

 
  if(alertTab[selObj.selChar]){
    alert(alertTab[selObj.selChar]);
    return true;
  }
  if(scanTab[selObj.selChar]){
    selObj.value=scanTab[selObj.selChar];
  }
}

That didn't do an alert at all when I entered =%8400
Then I changed the function to this:

function bar_bloodtype(e){
      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var keyCode = (e.which)?e.which:e.keyCode;
  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;
  var keyChar = String.fromCharCode(keyCode);
  var selObj = (e.target)?e.target: e.srcElement;
  selObj.selChar += keyChar;
 
  alert( selObj.selChar.charAt(1) + " " + selObj.selChar )

 
  if(alertTab[selObj.selChar]){
    alert(alertTab[selObj.selChar]);
    return true;
  }
  if(scanTab[selObj.selChar]){
    selObj.value=scanTab[selObj.selChar];
  }
}

and it returned "5" when I entered =%8400.
0
 

Author Comment

by:electricd7
ID: 21809667
I did some reading about how the ISBT128 labels are created.  They all include a 2-character identifier before the code.  In this label its the "=%".  From what I read, they are sent as ACSII 61 character.  So the Code 128 value of "%" is 5, and the ASCII value is 37.  So that might explain why I always got an alert of 5 when I sent it a %?  Not sure if that makes any sense to me or not, but I want to think I am on to something.  Does that help you at all?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21809808
>> In this label its the "=%".  
Well, I figured that =% was some sort of escape sequence, just like there is an escape sequence for UTF encoding, but that does not help understand why only 5 is alerted. In this:
alert( selObj.selChar.charAt(1) + " " + selObj.selChar )

selObj.selChar should reveal more than just 5. Now I am curious as to what:
selObj.selChar.charAt(0) and selObj.selChar.charAt(2) and selObj.selChar.charAt(3) will reveal when alerted.

>>From what I read, they are sent as ACSII 61 character.
Where did you read this?

>>So the Code 128 value of "%" is 5, and the ASCII value is 37
Not sure what you mean by "Code 128", but if you mean ASCII (which consists of 128 chars), 5 is not code for %.
0
 

Author Comment

by:electricd7
ID: 21812697
Yea the =% is not any type of exit sequence in this example.  When I scan the same label into a TEXT field and use the onBlur="setTimeout('bar_bt(document.form1)',1)" in the tag in conjunction with the following function:

function bar_bt(f){
  if (Left(f.compon.value, 2)=="=%"){
        alert(Right(f.compon.value, 4))
  }
  else{
        return;
  }
}

It works perfectly and alerts me with the 4-digit code that follows the "=%".  So the % in this case is definately just a % sign and nothing else.  The documentation I am speaking of is the actual ISBT128 Technical Specification for the project I am working on.  I think i will close this topic and start a new one, since I have a better understanding of whats going on now and what I need (at least I think I do!)
0
 

Author Comment

by:electricd7
ID: 21813927
OK hielo...you were right.  It has something to do with the encoding!  I looked that the code and noticed these 2 lines:

  if(keyCode>95&&keyCode<106) keyCode-=48;
  if(keyCode<48||keyCode>57) return true;

Well that basically says that if the keycodes are >95 and less than 106, then subtract 48 from it.  And then if they are <48 or >57 to ignore the scan and return true.  So I got to thinking, maybe the keycodes sent by the new labels are outside of that scope.  So I commented those lines out, and added:

alert(keyCode)

Then scanned one of the "=%" labels.  It alerted 187.  So that would have ignored in the past and returned true (exactly what it was doing.)  I then looked up ASCII 187 and it appears to be W, however when I filled in another text field with the String.fromCharCode value it appeared as ».  

So my question is this, how do I extract all of the keycodes sent by the scan instead of just the first one?  After I see them, I should be able to just add that string to the front of the numbers and be done, right?
0
 
LVL 82

Expert Comment

by:hielo
ID: 21814177
>>So I got to thinking, maybe the keycodes sent by the new labels are outside of that scope.
Yes

>> It alerted 187.
That's the key. I don't know why your subtracting 48 this is what you know so far:
a. When you are scanning CODABAR you are subtracting 48 (for whatever reason)
b. When you do NOT subtract 48 AND you send =% you get 187

So what you need to do is something like:
...
if( keyCode ==187)//ISBT128
{
  /* here you may have to subtract some factor (not sure what that would be) in the same sense that you are subtracting 48 for CODABAR */
}
else//CODABAR
{
  //here you subtract the 48
}
0
 

Author Comment

by:electricd7
ID: 21814313
well i'm only subtracting the 48 if its greater than 95 and less than 106 (which its not ever, from what I can tell).  I think this is an old line that can be removed.  Stay with me on this, I know its tedious, but its important to me that it gets figured out and I really appreciate your help.  

So now when i scan CODABAR I get 510 in a text field, and the alert alert(selObj.selChar) for that scan is "53" which is ASCII for 5, so thats good.  When i scan ISBT 8400, I get 187.  My question now is, how can I get the ASCII codes for the entire scan, rather than just the first character.  I am not opposed to re-writing the whole function, but I am just so lost at this point.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21815173
Well, ascii does not go beyond 128. So, if keyCode exceeds 128 most likely you have ISBT8400. So if
5=53 in CODABAR
and
5=187 in ISBT
the difference is 143. Whenever you scan, if the code exceeds 128, then subtract 134. To make things more clear, you want to "cast down" the ISBT code to ASCII code: 187 - 134 == 53. Similarly if you scan 627
6=54 ASCII
6=188 ISBT(or at least, it should)

188-134==54=="6"ASCII
0
 

Author Comment

by:electricd7
ID: 21815403
Yea that makes sense, but I don't think I made myself clear as to what I am scanning.  In CODABAR I am scanning O+ which is "51".  In ISBT I am scanning 0+ which is "=%5100".  The number being reported as 187 in ISBT is not the 5 from the 5100, but rather the "=" which is being interpreted as ASCII 187.  What I need to do is to find out what the ASCII codes from each of the 6 characters in the ISBT label are being interpreted as.  I know the "=" = 187, but now I need to find out the %, 5, 1, 0, 0.  Once I see this, I can fix it I think.  Any idea how to loop through the entire scan and see all 6 characters?  
0
 

Author Comment

by:electricd7
ID: 21815455
I am basing my understanding of the CODABAR scan on the ASCII table, BTW.  The "53" that comes across as the first character in the CODABAR scan is really ASCII 53, or 5 in text.  So the next code in the CODABAR scan should be ASCII 49 or "1".  Just not sure how to see each character in either?
0
 

Author Comment

by:electricd7
ID: 21815769
Hielo -

Just found the original post where this function came from.  It can be found here:

http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_21334979.html

At this point, what I want to do is remove the part where it subtracts 48 and remove the line where it checks to be sure its between 48 and 57 (ie, numeric).  Then basically If it comes upon a keyCode of 187, skip that character and don't add it to the selChar variable.  The next step would be to alert what the next character in the scan is.  We know the first is ASCII 187, but don't know the 2nd.  Once we know the second, we could alter the function to skip if it finds 187 or X (from the second character), then after that it should be numeric.  You follow:?
0
 

Author Comment

by:electricd7
ID: 21815882
YES!  I am getting somewhere.  One more stumbling block and I got it.  I made a function which looped through the scan and put the keyCodes transmitted in another text box.  I found that all my ISBT codes begin with the following 3 keyCodes:
187, 16, 53

So I added the following lines to the function:

  if (keyCode!=187&&keyCode!=16&&keyCode!=53){
        var keyChar = String.fromCharCode(keyCode);
        var selObj = (e.target)?e.target: e.srcElement;
        selObj.selChar += keyChar;
  }else{
      var selObj = (e.target)?e.target: e.srcElement;
  }

Well that works PERFECT except for 1 problem.  keyCode 53 is the number 5, and so when any label containing the number 5 is scanned (like O+ is 51) it ignores that character.  What I want to do is alter the function and ignore that keyCode if and only if it is preceeded by keyCode 187 and keyCode 16.  That would indicate that it is an ISBT scan.  Can you help??
0
 
LVL 82

Expert Comment

by:hielo
ID: 21816458
>>What I want to do is alter the function and ignore that keyCode if and only if it is preceeded by keyCode 187 and keyCode 16.
since you have selObj.selChar += keyChar on the code you posted, do you know if infact selObj.selChar "accumulates" characters. If so you can do:
if( keyCode==53 && selObj.selChar.length && selObj.selChar.charCodeAt(0)==187 &&selObj.selChar.charCodeAt(0)==16)
{
 null;//ignore keyCode 53
}
/*shouldn't these be || instead of &&? When you press a key, it will either 187 OR 16 OR 53. It CANNOT be all three at the same time.
*/
else  if (keyCode!=187 || keyCode!=16 || keyCode!=53){
        var keyChar = String.fromCharCode(keyCode);
        var selObj = (e.target)?e.target: e.srcElement;
        selObj.selChar += keyChar;
  }else{
      var selObj = (e.target)?e.target: e.srcElement;
  }
0
 

Author Comment

by:electricd7
ID: 21816659
yes you are right..they should be ||, doh!  Long day.  Also, the .charCodeAt(0) is simple the "1" in the 187, so I would have to use the whole keyCode to get the entire value.

>>if( keyCode==53 && selObj.selChar.length && selObj.selChar.charCodeAt(0)==187 &&selObj.selChar.charCodeAt(0)==16)

this seems to say if they key is 53 AND length? AND key is 187 ANd key is 16, then ignore.  Not sure what that line is supposed to do?  I thought about creating a second variable like selChar to hold a counter (FmtChk).  Basically I would increment the counter by 1 if I see 187 then return, then increment it again if I see 16, then return.  Then i could ignore a key of 53 if the counter = 2, otherwise I would use it.  Make sense?  Here is what I am trying, but having some problem (starting to get large.)  What do you think:

function bar_bloodtype(e){
      var scanTab = {
      "62":"A+",
      "06":"A-",
      "73":"B+",
      "17":"B-",
      "84":"AB+",
      "28":"AB-",
      "51":"O+",
      "95":"O-"
      };
      
      var alertTab = {
      "39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",
      "40":"Auto unit.",
      "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."
      }
  var selFmt = (e.target)?e.target: e.srcElement;
  var keyCode = (e.which)?e.which:e.keyCode;
  if (keyCode!=187||keyCode!=16){
      if(keyCode==53){
            if(selFmt.FmtChk==''||selFmt.FmtChk==3){
                    var keyChar = String.fromCharCode(keyCode);
                    var selObj = (e.target)?e.target: e.srcElement;
                    selObj.selChar += keyChar;
      
                  if(alertTab[selObj.selChar]){
                        alert(alertTab[selObj.selChar]);
                  return true;
                  }
                  if(scanTab[selObj.selChar]){
                        selObj.value=scanTab[selObj.selChar];
                  }
            }else{
                  selFmt.FmtChk += 1;
                  return true;
            }
      }else{
                    var keyChar = String.fromCharCode(keyCode);
                    var selObj = (e.target)?e.target: e.srcElement;
                    selObj.selChar += keyChar;
      
                  if(alertTab[selObj.selChar]){
                        alert(alertTab[selObj.selChar]);
                  return true;
                  }
                  if(scanTab[selObj.selChar]){
                        selObj.value=scanTab[selObj.selChar];
                  }
      }
  }else{
      selFmt.FmtChk += 1;
      return true;
  }
}
0
 

Author Comment

by:electricd7
ID: 21818947
Finally got this working.  I have attached the form field and the script below.  Basically I set up another variable called FmtChk which is set to 0 onfocus.  Then each key that is entered is checked to see if it is either 187, 16, or 53.  If its one of the first 2, it is ignored and FmtChk is immediately incremented by 1.  If the character is 53 (ASCII 5), then it does 1 of two things.  It looks to see if FmtChk is 0, if it is, that means the 5 was the sent before either 187 or 16 meaning it is likely a CODABAR scan and to go ahead and accept this character.  If its not 0, it checks to see if its 3 which would mean that the 187, 16, and now the 53 has already been sent, so this must be an ISBT scan and the 5 should be accepted since its not part of the initial prefix.  Each time the select box is focused on, the 2 variables used by this function (FmtChk, and selChar) are both set to null or 0.  The whole trick was to figure out that the scan was sending some kind of weird encoding, and once that was figured out, it was as simple as checking which string is sent with every ISBT label.  Heilo, thanks for all the help, I am awarding you the full 500 points for your suggestion that the encoding might be different.
<select name="BloodType" tabindex="4" onFocus="this.selChar=''; this.FmtChk=0;" onKeyDown="bar_bloodtype(event)" >

          <option value=""></option>

          <option value="A+">A +</option>

          <option value="A-">A neg</option>

          <option value="B+">B +</option>

          <option value="B-">B neg</option>

          <option value="AB+">AB +</option>

          <option value="AB-">AB neg</option>

          <option value="O+">O +</option>

          <option value="O-">O neg</option>

</select>
 

function bar_bloodtype(e){

//ISBT prefix is ASCII: 187 16 53 followed by 4 digits making up the bloodtype, then ASCII(9)

  var scanTab = {

	"62":"A+",

	"06":"A-",

	"73":"B+",

	"17":"B-",

	"84":"AB+",

	"28":"AB-",

	"51":"O+",

	"95":"O-"

  };

	

  var alertTab = {

	"39":"Unit is coded as HOLD!  Please contact CIRBC immediately.",

	"40":"Auto unit.",

    "47":"Unit is coded as BIOHAZARD!  Please contact CIRBC immediately."

  }

  

  var selFmt = (e.target)?e.target: e.srcElement;

  var keyCode = (e.which)?e.which:e.keyCode;

  

  if (keyCode==187||keyCode==16||keyCode==53){

	if(keyCode==53&&(selFmt.FmtChk==0||selFmt.FmtChk==3)){

		//Legit ASCII(53) count it

		var keyChar = String.fromCharCode(keyCode);

		var selObj = (e.target)?e.target: e.srcElement;

		selObj.selChar += keyChar;

		if(alertTab[selObj.selChar]){

		  alert(alertTab[selObj.selChar]);

		  return true;

		}

		if(scanTab[selObj.selChar]){

		  selObj.value=scanTab[selObj.selChar];

		}

	}else{

		//Last ASCII(53) in ISBT prefix, ignore it

		selFmt.FmtChk += 1;

		return true;

	}

	selFmt.FmtChk += 1;

	return true;

  }else{

  	var keyChar = String.fromCharCode(keyCode);

  	var selObj = (e.target)?e.target: e.srcElement;

  	selObj.selChar += keyChar;

	if(alertTab[selObj.selChar]){

	  alert(alertTab[selObj.selChar]);

	  return true;

	}

	if(scanTab[selObj.selChar]){

	  selObj.value=scanTab[selObj.selChar];

	}

  }

}

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now