Untitled

 avatar
unknown
plain_text
3 years ago
2.8 kB
3
Indexable
function generate_cf(){

    var cf = ""
    var surname = document.getElementById("surname")
    var name = document.getElementById("name")
    var comune = document.getElementById("cf_comune")
    var birth = document.getElementById("age")

    //Controllo dei valori
    var bools = []
    bools.push(validate_generic(surname))
    bools.push(validate_generic(name))
    bools.push(validate_cf_comune())
    bools.push(validate_age())
    bools.push(validate_sex())

    var checker = arr => arr.every(v => v === true);
    console.log(bools)
    if (checker(bools) == false){
        return false;
    }

    surname = surname.value.toUpperCase()
    name = name.value.toUpperCase()
    comune = comune.value.toUpperCase()
    birth = birth.value
    //Prendere le consonanti del cognome
    for (let x of surname.matchAll(/([^a|e|i|o|u|])/gi)){
        cf +=surname[x.index]
    } 
    //Se il cognome presenta meno di tre consonanti aggiungere vocali
    if (cf.length < 3){
        var vocaliNecessarie = 3 - cf.length 
        //Prendere tutte le vocali
        var vocali = ""
        for (let x of surname.matchAll(/([a|e|i|o|u|])/gi)){
            vocali += (surname[x.index])
        }
        cf += vocali.slice(0,vocaliNecessarie)

        if (cf.length < 3){
            for (var i=cf.length-1; i<4-cf.length; i++){
                cf += "X"
            }
        }
    }
    
    var newName = ""
    //Prendere le consonanti del nome
    for (let x of name.matchAll(/([^a|e|i|o|u|])/gi)){
        newName += name[x.index]
    }
    if (newName.length >= 4){
        newName = newName[0]+newName[2]+newName[3]
    }
    else if (newName.length < 3){
        var vocali = ""
        for (let x of name.matchAll(/([a|e|i|o|u|])/gi)){
            vocali += name[x.index]
        }
        newName += vocali.slice(0,vocaliNecessarie)
        
        if (newName.length < 3){
            for (var i=newName.length-1; i<4-newName.length; i++){
                newName += "X"
            }
        }
    }
    cf += newName

    //ANNO
    cf += birth.slice(2,4)
    
    //MESE
    var months = ['A','B','C','D','E','H','L','M','P','R','S','T']
    var monthtemp = birth.slice(5,7)
    if (monthtemp[0] == "0"){
        monthtemp = monthtemp[1]
    }
    monthtemp = parseInt(monthtemp)
    cf += months[monthtemp-1]

    //GIORNO
    var day = 0
    if (document.querySelector('input[name="sex"]:checked').value == 'female'){
        day += 40
    }
    day += parseInt(birth.slice(8,10))
    day = day.toString()
    if (day.lenght == 1){
        day = "0" + day
    }
    cf += day
    
    //CATASTALE
    cf += dict[comune]
    
    //ULTIMA LETTERA
    cf += "Z"
    var cf_form = document.getElementById("cf")
    cf_form.value = cf
    return turnWhite(cf_form)

}
Editor is loading...