/    Sign up×
Community /Pin to ProfileBookmark

javascript validation

I need a javascript function that would take a number, and multiply each of the digits alternately by 2 resp. 1, add the result of the multiplication. Note: If the result of a multiplication is two digit number (i. e. 14), add not 14 but 1 + 4 =5 ). Round up the result of the additions to the next 10th and note the difference between the next 10th and the result of the addition in the check digit.
Always start multiplying the second to last digit (i.e. 14 digit at 15 digit numbers and 15th digit at 16 digit numbers, respectively) by 2 and then alternate, multiplying by 1 and 2 proceeding to the left. Do not start with the first digit and start multiplying by 2.
Below are examples for the calculation for the 15 digit customer number

A 15 digit customer number, the first 14 digits are 99200300435738

2 x 8 = 16, 1 x 3 = 3, 2 x 7 = 14, 1 x 5 = 5, 2 x 3 = 6, 1 x 4 = 4
2 x 0 = 0, 1 x 0 = 0, 2 x 3 = 6, 1 x 0 = 0, 2 x 0 = 0, 1 x 2 = 2,
2 x 9 = 18, 1 x 9 = 9

Add 7+3+5+5+6+4+0+0+6+0+0+2+9+9 = 56
Round up to 60
Subtract 56 from 60 = 4
Therefore the check digit is 4 => 992003004357384

Can anyone help?

Thanks

Kati

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@UltimaterApr 05.2005 — [code=html]
<script type="text/javascript">
function check_digit(){
var pattern=[1,2]
var out_sum=0
for(var i=0;i<arguments[0].length;i++){
var temp1=arguments[0].charAt(i)*1
var temp2=pattern[i%2]
var temp3=temp1*temp2
if(temp3>9){
temp3+=""
temp3=(temp3.charAt(0)*1)+(temp3.charAt(1)*1)
}
out_sum+=temp3
}
out_sum=(Math.ceil(out_sum/10)*10)-out_sum
return out_sum
}
alert(check_digit("99200300435738"))
</script>
[/code]
Copy linkTweet thisAlerts:
@UltimaterApr 05.2005 — Is it a problem if my code starts evaluating the customer's number from left to right?

It shouldn't make any difference if all of them have the same length.

i.e. my code always evaluates the first digit on the left and multiplies it by 1 (then the next digit 2 and so on).
Copy linkTweet thisAlerts:
@UltimaterApr 05.2005 — If it [b]does[/b] make a difference, then use this function instead:
[code=html]
<script type="text/javascript">
function check_digit(){
var out_sum=0
var pattern=[1,2]
for(var i=arguments[0].length,j=0; i>=0; i--,j++){
var temp1=arguments[0].charAt(i)*1
var temp2=pattern[j%2]
var temp3=temp1*temp2
if(temp3>9){
temp3+=""
temp3=(temp3.charAt(0)*1)+(temp3.charAt(1)*1)
}
out_sum+=temp3
}
out_sum=(Math.ceil(out_sum/10)*10)-out_sum
return out_sum
}
alert(check_digit("99200300435738"))
</script>
[/code]
Copy linkTweet thisAlerts:
@katibogarauthorApr 06.2005 — thanks a lot
Copy linkTweet thisAlerts:
@Warren86Apr 06.2005 — <HTML>

<Head>

<Script Language=JavaScript>

function calcCheckDigit(custNum){

isValid = true;
if (custNum.length != 15 && custNum.length != 16 || isNaN(custNum) || custNum == "")
{
alert('Invalid customer number');
isValid = false;
document.forms.Form1.custNumber.value="";
document.forms.Form1.custNumber.focus();
}
if (isValid)
{
refStr = custNum.substring(0,custNum.length-1).split('').reverse().join('');
checkSum = 0;
x2Digits = "";
x1Sum = 0;
x2extendedSum = 0;
for (i=0; i<refStr.length; i+=2)
{x2Digits += refStr.charAt(i)}
for (i=1; i<refStr.length; i+=2)
{x1Sum += parseInt(refStr.charAt(i))}
for (i=0; i<x2Digits.length; i++)
{
tmpCalc = x2Digits.charAt(i)*2;
if (tmpCalc > 9){tmpCalc = tmpCalc-9}
x2extendedSum += tmpCalc;
}
grossSum = x2extendedSum+x1Sum;
checkSum = Math.abs(Math.round((grossSum)/10)*10-grossSum);
document.forms.Form1.inclCheckSum.value = custNum+checkSum;
}
}


</Script>

</Head>

<Body>

<Form name='Form1'>

Customer Number: (15 or 16 digits) <input type=text name='custNumber' size='20' value='992003004357382' onblur="calcCheckDigit(this.value)"><br><br>

Checksum String: <input type=text name='inclCheckSum' size='20' readonly>

</Form>

</Body>

</HTML>
Copy linkTweet thisAlerts:
@katibogarauthorApr 08.2005 — thanks warren!
×

Success!

Help @katibogar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...