I have a javascript function, which I need to run serverside using Coldfusion8.
The javascript function uses charCodeAt to check entered values (ILN, 13-digit values, for example 1234567891231) and computes a checkVal:
checker=0;
for (i=11; i>0; i=i-2){
checker += (iln.charCodeAt(i)-48)*3;
checker += iln.charCodeAt(i-1)-48;
}
checkVal = 10 - (checker%10);
if(checkVal==10){
checkVal=0;
}
if(checkVal != iln.charAt(12)){
alert("error, should be" + checkVal);
}
In Coldfusion I'm trying to do this with a loop, mid() and asc(), but I can't get it to work. Here is what I have:
<cscript>
var checkValue = 1234567891231;
var done = "";
</cscript>
<cfloop from="11" to="1" index="i" step="-2">
<cfscript>
check = check + (ASC(MID(checkValue,#i#,1))-48)*3;
check = check + ASC(MID(checkValue,(#i#-1),1))-48;
</cfscript>
</cfloop>
<cfset done = 10 - check/10>
<cfif done EQ 10><cfset done = 0></cfif>
<cfif done NEQ mid(checkValue,12,1)>
<cfscript>
// error handler
</cfscript>
</cfif>
The values are passed in correctly but my ASC(MID()) is producing an error and I have not found out what I'm doing wrong.
Can someone give me a pointer?
Thanks!
EDIT: updated CF loop parameters
EDIT2: pinned it down to the 2nd mid-function getting 1-1=0 on the last loop iteration.