Results 1 to 8 of 8

Thread: javascript validation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    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

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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;
    }
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    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

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: javascript validation

    Brilliant, thanks a lot!


    kati

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Unhappy 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?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2005
    Posts
    107

    Re: javascript validation

    i solved the problem. thanks

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: javascript validation

    Was it the code or the numbers?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width