Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public static bool ValidateDigit(string sTrn)
   {
       if (sTrn != "")
       {
           if (sTrn.Length < 9) return false;
           //this function multiplies digits 1,4,7 by 3, digits 2,5,8 by 7, and 3,6,9 by 1, and
           //adds the results together. If the total is evenly divisible by 10, then the check digit is ok
           //otherwise, it is not.  example "good" trn = 263181368

           int[] multipliers = new int[3] { 3, 7, 1 };  //multiply each digit by one of these numbers
           int s = 0;
           int m = 0;

           for (int i = 0; i < 9; i++)
           {
               s += int.Parse(sTrn.Substring(i, 1)) * multipliers[m];
               m = ++m < 3 ? m : 0;  //reset to first multiplier
           }

           return (s != 0 && s % 10 == 0) ? true : false;
       }
       else
           return false;
   }
Posted
Comments
Shemeemsha (ഷെമീംഷ) 29-Oct-14 6:59am    
Use this same logic their ..
Any problem ?
sharonjoy 29-Oct-14 7:18am    
Didnt workout.How could i initialize ' int[] multipliers = new int[3] { 3, 7, 1 }; ' in jquery.?
Sunita Rana 29-Oct-14 7:42am    
for initializing array in javascript see this sample:

<!DOCTYPE html>
<html>
<body>

<p id="demo"></p>

<script>
var numbers= [1, 1, 46];
document.getElementById("demo").innerHTML = numbers[2];
</script>

</body>
</html>

Hi sharonjoy,

Here I have created one jQuery method as per your logic, please check this out :-

JavaScript
function ValidateNumber(strNo) {
            if (strNo == '' || strNo.length < 9) return false;

            var b = false;
            var s = 0;
            var m = 0;
            var multipliers = [3, 7, 1];

            $.each(strNo, function (i, n) {
                if (n == 1 || n == 4 || n == 7) s += (n * 3);
                if (n == 2 || n == 5 || n == 8) s += (n * 7);
                if (n == 3 || n == 6 || n == 9) s += (n * 1);
            });
            b = (s != 0 && s % 10 == 0) ? true : false;

            return b;
        }



Hope this will definitely of help to you.
 
Share this answer
 
Comments
sharonjoy 30-Oct-14 1:22am    
m getting an error when i tried.
http://localhost:13109/css/menu/jquery-1.10.2.min.js
0x800a138f - JavaScript runtime error: Invalid operand to 'in': Object expected
SRS(The Coder) 30-Oct-14 2:18am    
This error is due to not placing the js file at proper place or you need to change the path to proper one where exactly the jQuery library file is placed actually.
sharonjoy 30-Oct-14 1:46am    
I have simplified your answer and i got what i looking for.Thanks everyone
C#
var x = new Array(3, 7, 1);
       var s = 0;
       var m = 0;
       var j = 0;
       for (i = 0; i < 9; i++) {

           if (m == 0)
               j = 3;
           else if (m == 1)
               j = 7;
           else if (m == 2)
               j = 1;

           s += parseInt(vNumbr[i], 10) * j;

           m = ++m;

           if (m >= 3) {
               m = 0
           }

       }
       return (s != 0 && s % 10 == 0) ? true : false;
 
Share this answer
 
Comments
SRS(The Coder) 30-Oct-14 2:18am    
thanks for sharing.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900