Untitled

 avatar
unknown
plain_text
a year ago
5.9 kB
6
Indexable
   this.checkCodiceFiscale = function () {
       debugger
       var codiceFiscale = formContext.getAttribute("yod_codicefiscale").getValue();
       // check if the codice fiscale is valid
       
           // Given a Codice Fiscale, verify the control code
           codiceFiscale = codiceFiscale.toUpperCase();
       if (codiceFiscale.length !== 16)

               return false; // error
           else {
               if (codiceFiscale.substring(15, 16) ===
                   calcolaCodiceControllo(codiceFiscale.substring(0, 15)))
                   return true;
               else
                   return false;
           }
   }
   // Calculate the control code
   function calcolaCodiceControllo(codice) {
       // Calcola il codice di controllo del Codice Fiscale
       // Input: i primi 15 caratteri del Codice Fiscale
       // Output: il codice di controllo
       let contatore = 0;
       codice = codice.toUpperCase();
       if (codice.length !== 15)
           return "0"; // zero: errore
       else {
           for (let i = 0; i < codice.length; i++) {
               contatore += valoreDelCarattere(codice.substring(i, i + 1), i);
           }
           contatore %= 26; // si considera il resto
           return String.fromCharCode(contatore + 65);
       }
   }
   // Get the value of a character
   function valoreDelCarattere(carattere, posizione) {
       let valore = 0;
       switch (carattere) {
           case "A":
           case "0":
               valore = (posizione % 2) === 0 ? 1 : 0;
               break;
           case "B":
           case "1":
               valore = (posizione % 2) === 0 ? 0 : 1;
               break;
           case "C":
           case "2":
               valore = (posizione % 2) === 0 ? 5 : 2;
               break;
           case "D":
           case "3":
               valore = (posizione % 2) === 0 ? 7 : 3;
               break;
           case "E":
           case "4":
               valore = (posizione % 2) === 0 ? 9 : 4;
               break;
           case "F":
           case "5":
               valore = (posizione % 2) === 0 ? 13 : 5;
               break;
           case "G":
           case "6":
               valore = (posizione % 2) === 0 ? 15 : 6;
               break;
           case "H":
           case "7":
               valore = (posizione % 2) === 0 ? 17 : 7;
               break;
           case "I":
           case "8":
               valore = (posizione % 2) === 0 ? 19 : 8;
               break;
           case "J":
           case "9":
               valore = (posizione % 2) === 0 ? 21 : 9;
               break;
           case "K":
               valore = (posizione % 2) === 0 ? 2 : 10;
               break;
           case "L":
               valore = (posizione % 2) === 0 ? 4 : 11;
               break;
           case "M":
               valore = (posizione % 2) === 0 ? 18 : 12;
               break;
           case "N":
               valore = (posizione % 2) === 0 ? 20 : 13;
               break;
           case "O":
               valore = (posizione % 2) === 0 ? 11 : 14;
               break;
           case "P":
               valore = (posizione % 2) === 0 ? 3 : 15;
               break;
           case "Q":
               valore = (posizione % 2) === 0 ? 6 : 16;
               break;
           case "R":
               valore = (posizione % 2) === 0 ? 8 : 17;
               break;
           case "S":
               valore = (posizione % 2) === 0 ? 12 : 18;
               break;
           case "T":
               valore = (posizione % 2) === 0 ? 14 : 19;
               break;
           case "U":
               valore = (posizione % 2) === 0 ? 16 : 20;
               break;
           case "V":
               valore = (posizione % 2) === 0 ? 10 : 21;
               break;
           case "W":
               valore = 22; // Since both cases result in 22
               break;
           case "X":
               valore = (posizione % 2) === 0 ? 25 : 23;
               break;
           case "Y":
               valore = 24; // Since both cases result in 24
               break;
           default:
               valore = 0;
               break;
           case "Z":
               valore = (posizione % 2) === 0 ? 23 : 25;
               break;
           default:
               valore = 0;
               break;
       }
       return valore;
   }


   this.validateAndCorrectPhoneNumber = function (phoneNumber) {
       var phoneNumber = formContext.getAttribute("telephone1").getValue();

       const cleanedPhoneNumber = validateItalianPhoneNumber(phoneNumber);

       // Check if the cleaned number contains non-digits or has an incorrect number of digits
       if (!/^\d{10}$/.test(cleanedPhoneNumber)) {
           throw new Error('Invalid Italian phone number');
       }

       return cleanedPhoneNumber;
   }
   function validateItalianPhoneNumber(phonenumber) {

       var phoneNumber = formContext.getAttribute("telephone1").getValue();


       // Remove any non-digit characters from the phone number
       const cleanedPhoneNumber = phoneNumber.replace(/\D/g, '');
       // Check if the number starts with "+39" and transform it to "0039"
       if (cleanedPhoneNumber.startsWith('39')) {
           return '0039' + cleanedPhoneNumber.slice(2); // Return the corrected number
       }
       // Check if the number starts with "0039"
       if (!cleanedPhoneNumber.startsWith('0039')) {
           // If not, add "0039" to the beginning
           return '0039' + cleanedPhoneNumber; // Return the corrected number
       }
       // Return the original number if it already conforms to the correct format
       return cleanedPhoneNumber;
   }
Editor is loading...
Leave a Comment