Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
birth date input not working after number 13 with javascript

We are doing AB testing. We can only touch the client side code with Google Optimize. So the designer asked us to change the birth date format from separated input to an unify input like this ->

enter image description here

So the idea is that the user type the birth date numbers [13.07.1998] and then the information will get to the separated input like this [13] [July] [1998]

It works fine until we use day 13. After that number the code does not work. But if I use 12 it works.

enter image description here

const dobInput = document.querySelector("input");
const birthDay = document.querySelector("#birthDay");
const birthMonth = document.querySelector("#birthMonth");
const birthYear = document.querySelector("#birthYear");

dobInput.addEventListener("change", (e) => {
  const dobString = e.target.value;

  if ( isValidDate(dobString) ) {

    let date = dobString.slice(0, 2);
    let month = dobString.slice(3, 5);
    let year = dobString.slice(6, 10);

    birthDay.value = date;
    birthMonth.value = month;
    birthYear.value = year;

  } else {

    const errorMessage = document.querySelector(".cro-error-text");
    errorMessage.classList.add('error');
    
  }
});

function isValidDate(dateString) {
  // First check for the pattern
  if (!/^\d{1,2}\.\d{1,2}\.\d{4}$/.test(dateString)) return false;

  // Parse the date parts to integers
  var parts = dateString.split(".");
  var day = parseInt(parts[1], 10);
  var month = parseInt(parts[0], 10);
  var year = parseInt(parts[2], 10);

  // Check the ranges of month and year
  if (year < 1905 || year > 2004 || month == 0 || month > 12) return false;

  var monthLength = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  // Adjust for leap years
  if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
    monthLength[1] = 29;

  // Check the range of the day
  return day > 0 && day <= monthLength[month - 1];
}

Does anyone knows why it could be happening? thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think the error is this....

if-----------------------------------------

12 - - - 10 - - - 2000

Day - - - month - - - year

in your code you are saving

12 - - - - 10 - - - 2000

Month - - -Day - - - -year



I think the error is this because I think you are using selects tag.

select tags have options tag....and each option tag have a value.

<select>
<option value=1>Jenuary</option>
<option value=2>February</option>
</select>

So _select.value=13 is not posible.because there shouldn't be an

<option value=13>

There is not a month 13...I think you are confusing month with day when you store it in the variable


Or check if all options tags are within select tag...

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda