"perl validate id number" Code Answer's

You're definitely familiar with the best coding language Perl that developers use to develop their projects and they get all their queries like "perl validate id number" answered properly. Developers are finding an appropriate answer about perl validate id number related to the Perl coding language. By visiting this online portal developers get answers concerning Perl codes question like perl validate id number. Enter your desired code related query in the search bar and get every piece of information about Perl code related question on perl validate id number. 

perl validate id number

By Jakes_Bakes_CodeJakes_Bakes_Code on Apr 17, 2020
# NB--> Language: Perl
# NB--> Country of ID: South Africa (RSA)
use Scalar::Util 'looks_like_number';
sub Validate_IdNum($);
sub GetLuhnDigit($);

#----------------------------------------------------------------#
# Validates the id number being parsed and returns a boolean, 
# If the id number is valid, returns 0, otherwise returns 1
#      PARAMETERS
#                <sIdNo> The id number that needs to be validated
#      EXAMPLES
#               <IN:94091850160a0> <OUTPUT:[0]["Id number cannot contain numeric values"]>
sub Validate_IdNum($)
{
   my $sSubName = (caller(0))[3]; # This is just a reference to the function/subroutine name
   my $bFaulty = 0;

   my $sIdNo = $_[0];
   
   my $iStrLen = length $sIdNo;
   my $LastChar = substr($sIdNo, - 1);

   if ( looks_like_number($sIdNo) == 0 ) {
      $bFaulty = 1;
   }
   else {
      if ( (length $sIdNo) != 13 ) {
         $bFaulty = 1;
      }
      elsif ( substr($sIdNo, - 1) != GetLuhnDigit($sIdNo) ){
         $bFaulty = 1;
      }
   }

   if ($sRetMsg eq "") {
      $sRetMsg = "0";
   }

   return ($sRetMsg,$bFaulty);
}

#----------------------------------------------------------------#
# Used to validate rsa id numbers: (finds expected last digit given the first 12)
# {See the Luhn algorithm: https://en.wikipedia.org/wiki/Luhn_algorithm}
sub GetLuhnDigit($)
{
   my $sSubName = (caller(0))[3]; # This is just a reference to the function/subroutine name
   my $iLastDigit = -1;
   my $s12Digits = $_[0];
   my $iSumOdd = 0;
   my $iEvenDigits = "";

   for (my $i=0;$i<6;$i++) {
     $iSumOdd = $iSumOdd + substr($s12Digits,2 * $i,1);
   }

   for (my $i=0;$i<6;$i++) {
     $iEvenDigits = $iEvenDigits . substr($s12Digits,(2 * $i) + 1,1);
   }

   my $iDblEven = 2 * $iEvenDigits;
   my $iSumDblEvens = 0;
   
   for (my $i = 0; $i < length($iDblEven); $i++) {
      $iSumDblEvens = $iSumDblEvens + substr($iDblEven,$i,1);
   }

   my $iUnitVal = $iDblEven;
   my $iFinalBase = $iSumOdd + $iSumDblEvens;

   $iLastDigit = 10 - substr($iFinalBase, - 1);

   if ($iLastDigit == 10) {
      $iLastDigit = 0;
   }

   return $iLastDigit;
}

Add Comment

2

All those coders who are working on the Perl based application and are stuck on perl validate id number can get a collection of related answers to their query. Programmers need to enter their query on perl validate id number related to Perl code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about perl validate id number for the programmers working on Perl code while coding their module. Coders are also allowed to rectify already present answers of perl validate id number while working on the Perl language code. Developers can add up suggestions if they deem fit any other answer relating to "perl validate id number". Visit this developer's friendly online web community, CodeProZone, and get your queries like perl validate id number resolved professionally and stay updated to the latest Perl updates. 

Perl answers related to "perl validate id number"

View All Perl queries

Perl queries related to "perl validate id number"

Browse Other Code Languages

CodeProZone