7 Replies Latest reply on Apr 17, 2013 12:27 PM by gkaiseril

    Checking for Valid Canadian SIN Using Adobe XI

    newtoforms

      I am creating a form that request a SIN Number for a employee,

       

      I have used this to format the SIN and it is located in Custom Format Script.

       

      // custom format script

        (function() {

      if (event.value != "") {

      var RE_BP_Frmt = /^(\d{3,3})[.-/ ]?(\d{3,3})[.-/ ]?(\d{3,3})$/; // entry 000-000-000;

      if(RE_BP_Frmt.test(event.value)) {

      event.value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3;

      } // end re testing

      } // end custom format script

      } ) ();

      // end custom format script

       

      Once I have the SIN Number I have to check using a formula to see if it is a valid SIN. Now can I do this. Also if the user does not enter 9 digits they should get an error message, if they do not enter a valid SIN they should get an error message. If someone could please help me with it, it would be greatly appreciated it.

        • 1. Re: Checking for Valid Canadian SIN Using Adobe XI
          gkaiseril MVP & Adobe Community Professional

          Since the RegExp test has already determined that 9 numbers or 9 numbers and optional separators have been entered and valid by the "If ... then" statement, anything else is not valid. Just add an "else" clause to the if statement.

           

           

          // custom format script

            (function() {

          if (event.value != "") {

          var RE_BP_Frmt = /^(\d{3})[.-/ ]?(\d{3})[.-/ ]?(\d{3})$/; // entry 000-000-000;

          if(RE_BP_Frmt.test(event.value)) {

          event.value = RegExp.$1 + "-" + RegExp.$2 + "-" + RegExp.$3;

          } else {

          app.alert(event.value + " is not a valid Social Insurance Number. Please reenter.", 1, 0);

          }

          // end re testing

          } // end not null entry

          } ) ();

          // end custom format script

           

          You could even write a custom keystroke script for the SIN.

           

          • 2. Re: Checking for Valid Canadian SIN Using Adobe XI
            try67 MVP & Adobe Community Professional

            I think the easiest way of doing it is by using this customized format mask (Format - Special - Arbitrary Mask): 999-999-999

            This will only allow the user to enter a 9 digit number, and will automatically add the hyphens for them if they don't do it.

            It will also give a (generic) error message when a wrong value is filled-in, and will reject it.

            • 3. Re: Checking for Valid Canadian SIN Using Adobe XI
              newtoforms Level 1

              Thank you for your reply. I have been asked to use the Luhn Algorithm to verify the SIN.

              1. Counting from the check digit, which is the rightmost, and moving left, double the value of every second digit; if product of this doubling operation is greater than 10 (e.g., 7*2 = 14) then 9 should be subtracted from the product (e.g. 7*2-9 = 5).
              2. Sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5) together with the undoubled digits from the original number.
              3. If the total modulo 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

              I'm just not sure how to take what was entered on the form as a SIN and Validate it using the above rules with JavaScript.

               

              Again thank you for your reply.

              • 4. Re: Checking for Valid Canadian SIN Using Adobe XI
                newtoforms Level 1

                Thank you for your reply, but using the mask would not be helpful in this case. I was asked to use the Luhn algorithm to verify that it is a actual SIN number not that the value entered is correct. I would have posted that before but did not have that information until a little while ago. Thank you again for your help.

                • 5. Re: Checking for Valid Canadian SIN Using Adobe XI
                  gkaiseril MVP & Adobe Community Professional

                  You can use the following custom validation scirpt:

                   

                  // custom validation script;
                  function ValidateCNSIN(cValue) {
                  // validate Canadian Social Insurance Number;

                  // convert value to array;
                  var aValue = cValue.split("");
                  var nSum = 0;
                  var nNum;
                  for(i = 0; i < 9; i++) {
                  nNum = Number(aValue[i]);
                  // double odd number elements;
                  if(i % 2 == 1) nNum *= 2;
                  // reduce values greater than 9 to sum of digits;
                  if(nNum > 9) nNum = Math.floor(nNum / 10) + (nNum % 10);
                  // sum vlaues;
                  nSum += nNum;
                  }

                  // return results of modulo test ;
                  return ((nSum % 10) == 0);

                  } // end ValidateCNSIN function

                   

                  event.rc = true;
                  if (event.value != "") {
                  var RE_BP_Frmt = /^(\d{3})[. -]?(\d{3})[. -]?(\d{3})$/;
                  if(RE_BP_Frmt.test(event.value) == false) {
                  app.alert(event.value + " is not a valid Social Insurance Number. Please reenter.", 1, 0);
                  event.rc = false
                  } else {
                  if( ValidateCNSIN(RegExp.$1+RegExp.$2+RegExp.$3) == false) {
                  app.alert(event.value + "is not a valid Canda Social Insurance Number.", 0, 0);
                  event.rc = false
                  }
                  } // end RegExp test
                  }  // end not null value
                  // end custom validattion script

                   

                  And then any method for the custom format or image.

                  • 6. Re: Checking for Valid Canadian SIN Using Adobe XI
                    newtoforms Level 1

                    Thank you this works. I have only one more thing I need to add. If they enter a 000-000-000 the scrip does not pick this up as a invalid SIN. I would need to included some kind of If statement to catch it and promoted with a Invalid SIN NUM. Please reenter the SIN. I am not sure on how to write the if statment or where to place it.

                     

                    Thanks again,

                    • 7. Re: Checking for Valid Canadian SIN Using Adobe XI
                      gkaiseril MVP & Adobe Community Professional

                      Then you need to test for this value and not run the validation function when the field has that value.

                       

                      // custom validation script;
                      function ValidateCNSIN(cValue) {
                      // convert value to array;
                      var aValue = cValue.split("");
                      var nSum = 0;
                      var nNum;
                      for(i = 0; i < 9; i++) {
                      nNum = Number(aValue[i]);
                      // double odd number elements;
                      if(i % 2 == 1) nNum *= 2;
                      // reduce values greater than 9 to sum of digits;
                      if(nNum > 9) nNum = Math.floor(nNum / 10) + (nNum % 10);
                      // sum vlaues
                      nSum += nNum;
                      }
                      return ((nSum % 10) == 0); // return modulos test results;
                      }

                      event.rc = true;
                      if (event.value != "") {
                      var RE_BP_Frmt = /^(\d{3})[. -]?(\d{3})[. -]?(\d{3})$/;
                      if(RE_BP_Frmt.test(event.value) == false) {
                      app.alert(event.value + " is not a valid Social Insurance Number. Please reenter.", 1, 0);
                      event.rc = false
                      } else {
                      // valid format - now test for valid check digit;
                      if(event.value != "000-000-000") {
                      // value not "000-000-000" so run check digit test;
                      event.rc = ValidateCNSIN(RegExp.$1+RegExp.$2+RegExp.$3)
                      if(event.rc == false) {
                      // check digit test failed;
                      app.alert(event.value + "is not a valid Canda Social Insurance Number.", 0, 0);
                      } // end check digit failed message;
                      } // end not '000-000-000';
                      } // end RegExp test;
                      }  // end not null value;
                      // end custom validattion script;