ParseInt from input and use as check for sum of last value using Javascript

Trying to keep track of the numbers in the input value and use them as checkdigit for the last number. Basically, take the first nine digits of the input, run some simple math, and then add those numbers together. Take this total, divide by two, then use the remainder as the key figure.

Unsuccessful so far, so if anyone has a good idea of ​​where I am going, I would gladly appreciate it. I put a fiddle here: Das Fiddle

 window.onkeyup = keyup;
 var inputTextValue;

 function keyup(e) {
  inputTextValue = e.target.value;
  $('#numberValue').text(inputTextValue);
        // must be 10 characters long
        if (inputTextValue.length !== 10) {
            return false;
        }

        // run the checksum
        var valid = false;
        try {
            var sum = (parseInt(inputTextValue[0], 10) * 2) +
                (parseInt(inputTextValue[1], 10) * 3) +
                (parseInt(inputTextValue[2], 10) * 4) +
                (parseInt(inputTextValue[3], 10) * 2) +
                (parseInt(inputTextValue[4], 10) * 3) +
                (parseInt(inputTextValue[5], 10) * 4) +
                (parseInt(inputTextValue[6], 10) * 2) +
                (parseInt(inputTextValue[7], 10) * 3) +
                (parseInt(inputTextValue[8], 10) * 4);

            var checkNumber = 0;

            if ((sum % 10) > 0) {
                checkNumber = (sum % 10).toFixed(-1);
            }

            if (inputTextValue[9] === ("" + checkNumber)) {
                valid = true;
                alert(checkNumber)

            }
        } catch (e) {
            valid = false;
        }

        return valid;

      

}

+3


source to share


1 answer


You must use:

   checkNumber = (sum % 10).toFixed(0);

      



toFixed (-1) will return 0.

The spell is here

+1


source







All Articles