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

157
Vistas
Check if the value of textboxt exists in Typeahead

I have this line of codes that will get all of the location in my database when user type in my textbox. The problem is I want to validate when the user change the text in my textbox and doesnt exist in my typeahead?

var path = "{{ route('search.location') }}";

$('input.typeahead').typeahead({
    source:  function (query, process) {
        return $.get(path, { query: query }, function (data) {
            return process(data);
        });
    }
}).blur(function () {
    if(source.indexOf($(this).val()) === -1) 
        alert('Not exists');
});

I've done some research using .blur function but i cant get it work?

ReferenceError: source is not defined
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

This is an example with local data. Boolean variable something is set to true, if query at least matches one item.

$( document ).ready(function() {

var data = [
  {value: "Alabama"},
  {value: "Delaware"},
  {value: "Maine"}
];

var somethingFound = false;

$("#the-basics .typeahead").typeahead(
{
  minLength: 1,
  highlight: true 
},
{
  source: function(query, syncResults, asyncResults) {
      // use query to filter data
      var filteredData = data.filter(function(e){
        return e.value.toLowerCase().startsWith(query.toLowerCase());
      });
      console.log(filteredData.length);
      somethingFound = filteredData.length > 0;
    // return filtered data
    syncResults(filteredData);
   }
}).blur(function(){
  if (!somethingFound) {
    alert('nothing found');
  }
});
});

see also in Plunker

about 4 years ago · Santiago Trujillo 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