Tengo esta línea de códigos que obtendrán toda la ubicación en mi base de datos cuando el usuario escriba en mi cuadro de texto. El problema es que quiero validar cuando el usuario cambia el texto en mi cuadro de texto y no existe en mi escritura anticipada.
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'); }); He investigado un poco usando la función .blur pero no puedo hacer que funcione.
ReferenceError: source is not definedEste es un ejemplo con datos locales. La variable booleana algo se establece en verdadero, si la consulta al menos coincide con un elemento.
$( 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'); } }); });