jQuery UI Autocomplete ignore non alphanumeric letters
I want to ignore all non alphanumeric letters from autocomplete input. For example if user inputs K P COLLECTION
it can search K. P. COLLECTION
.
This is my code on http://jsbin.com/usupem
Code:
var autocomplete_data = data here...
$( ".autocomplete" ).autocomplete({
source: function(req, response) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp( "^" + re, "i" );
response($.grep(autocomplete_data, function(item){return matcher.test(item.label); }) );
},
focus: function( event, ui ) {
$( ".autocomplete" ).val( ui.item.label );
开发者_JS百科 return false;
},
select: function( event, ui ) {
$( ".autocomplete" ).val( ui.item.label );
return false;
}
}).data( "autocomplete" )._renderItem = function( ul, item ) {
return $( "<li></li>" ).data( "item.autocomplete", item ).append( "<a>" + item.label + "<br><small>" + item.desc + "</small></a>" ).appendTo( ul );
};
You should strip the non alpha-numeric characters from both the input and the term that you're matching. Try calling something like this on both the req.term and item.label values in your source function:
function stripNonAlphaNumeric(string){
var r = string.toLowerCase();
r = r.replace(new RegExp("[^A-z0-9 ]", 'g'), "");
return r;
}
http://jsbin.com/ufetiq/3
精彩评论