Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

250
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda