Dr. Domenico Giacomo Campanile

Sviluppatore App Web e Mobile

Calculate gender and date of birth from the Italian tax code ReactJS

const months = [
  { code: 'A', month: '01' },
  { code: 'B', month: '02' },
  { code: 'C', month: '03' },
  { code: 'D', month: '04' },
  { code: 'E', month: '05' },
  { code: 'H', month: '06' },
  { code: 'L', month: '07' },
  { code: 'M', month: '08' },
  { code: 'P', month: '09' },
  { code: 'R', month: '10' },
  { code: 'S', month: '11' },
  { code: 'T', month: '12' }
]
  inverseFiscalCodeCalculate = event => {
    var fiscalCode = event.target.value
    const { t } = this.props

    if (strongRegexFiscalCode.test(fiscalCode)) {

      var getNumberGender = fiscalCode.substring(9, 11)
      var getNumberDayDate = getNumberGender > 40 ? Math.abs(getNumberGender - 40) : getNumberGender
      var getNumberMonthDate = fiscalCode.substring(8, 9)
      var getNumberYearDate = fiscalCode.substring(6, 8)
      var filterMonth = months.filter(item => item.code.includes(getNumberMonthDate));

      var yearCheck = parseInt(getNumberYearDate) + 1900
      var dateCheck = new Date();
      var yearBorn = 0
      dateCheck.setFullYear(dateCheck.getFullYear() - 85);

      if (yearCheck < dateCheck.getFullYear())
        yearBorn = parseInt(getNumberYearDate)  + 2000
      else yearBorn = yearCheck

      this.setState({
        isFiscalCodeCorrect: true,
        infoUser: fcUtility.getFiscalCodeInfo(fiscalCode),
        gender: getNumberGender < 32 ? 'M' : 'F',
        bornDate: yearBorn + '-' + filterMonth[0].month + '-' + getNumberDayDate
      });

    }
    else {
      this.setState({
        isFiscalCodeCorrect: false,
      });
    }
  }
  <AvField
              type="text"
              name="fiscalCode"
              id="fiscalCode"
              label={"fiscalCode"}
              defaultValue={fiscalCode}
              onChange={this.inverseFiscalCodeCalculate}
              validate={{
                required: { value: true, errorMessage: 'Campo obbligatorio' },
                pattern: {
                  value: '^([A-Za-z]{6}[0-9lmnpqrstuvLMNPQRSTUV]{2}[abcdehlmprstABCDEHLMPRST]{1}[0-9lmnpqrstuvLMNPQRSTUV]{2}[A-Za-z]{1}[0-9lmnpqrstuvLMNPQRSTUV]{3}[A-Za-z]{1})$|^([0-9]{11})$',
                  errorMessage: t("Message.fiscalCodeError")
                },
                maxLength: { value: 16, errorMessage: 'Il codice fiscale deve essere 16 caratteri' }
              }}
            />
Open chat
Hai bisogno di assistenza?!
Ciao! sono il Dr. Domenico Giacomo Campanile esperto nello sviluppo di applicativi Web e Mobile.
Vuoi creare un App o un sito Web per la tua attività?! ti seguiremo a 360°! Progettazione, Sviluppo, Grafica e Pubblicizzazione sul Web!