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

160
Visualizações
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 Respostas
Responde à pergunta

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 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