Re: javascript validation
Sounds fun.
Code:
function addDigits(number)
{
var sum = 0;
do {
sum += number % 10;
number = Math.floor(number / 10);
} while(number != 0);
return sum;
}
function validateUserID(userID)
{
var lastDigit = userID % 10;
userID = Math.floor(userID /10);
var factor = 2;
var sum = 0;
do {
sum += addDigits((userID % 10) * factor);
factor ^= 3; // Bit pattern trick to flip between 1 and 2.
userID = Math.floor(userID /10);
} while(userID != 0);
var checkDigit = 10 - (sum % 10);
return checkDigit == lastDigit;
}
Re: javascript validation
thanks so much for your reply. the outcome of your code is always 10. For 99200300435738 it should be 4. I can't figure out what's wrong with it.
Kati
Re: javascript validation
The script doesn't just compute the check number, but takes the full 15 digits of the user ID, computes the check number out of the first 14 and compares it to the last, returning true if they match.
validateUserID(992003004357384)
returns true.
Re: javascript validation
Brilliant, thanks a lot!
kati
Re: javascript validation
it seemed to work really well but now i found a few numbers that are supposed to be valid but the code rejects them (992003020632810 or 992223020632830)
It would be brilliant if you could have a look at this. Is it the code or the numbers arent valid?
Re: javascript validation
i solved the problem. thanks
Re: javascript validation
Was it the code or the numbers?