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

139
Vistas
JQuery text function isn't showing the text

I am using JQuery on laravel 8 and I am trying to show "This field is required." when the date field becomes empty. However, when I remove the default value (todays date) nothing shows. If I put the date in the future, "The date can't be in the future." shows no bother. I can't find out why. It is entering the "if (value === "")"

jquery

$(document).on("change", "#date", function(){
var value = $("#date").val();
if (value === ""){

  //this line doesn't show anything
  $("#dateError").text("This field is required.").css('color', '#ff0000').attr('class', 'col-6');
} else{
  $("#dateError").text("");
}
let today = new Date();
let inputDate = new Date(value);
if (inputDate > today){

// this will show
   $("#dateError").text("The date can't be in the future.").css('color', '#ff0000').attr('class', 'col-6');
} else{
   $("#dateError").text("");
}
})

html

<div class="row m-3">
  <label for="date" class="col-lg-2 me-2">Date<i>*</i></label>
  <input type="date" id="date" name="date" value="{{ old('date', now()->toDateString()) }}" class="col-lg-9"/>
</div>
<div class="row m-3">
  <span id="dateError"></span>
</div>
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The basic problem is you are running the second if() regardless of what happens in the first and the second will overwrite text set in the first.

One fix would be to return when value is empty and after the error text has been set

if (value === ""){
  
  $("#dateError").text("This field is required.").css('color', '#ff0000').attr('class', 'col-6');

   // don't go any further 
   return;
} else ...
about 4 years ago · Juan Pablo Isaza Denunciar

0

Maybe because of ===. Value is a date object, and you are comparing it to a string. And for the today variable, you created an instance only.

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