Skip to content

Commit

Permalink
Autocomplete (combobox demo): Detect valid entries when typed, but no…
Browse files Browse the repository at this point in the history
…t selected from menu. Fixes #5605 - Autocomplete combobox demo does not accept valid values.
  • Loading branch information
scottgonzalez committed Jul 20, 2010
1 parent ba09165 commit 5d1e297
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions demos/autocomplete/combobox.html
Expand Up @@ -29,7 +29,7 @@
delay: 0,
minLength: 0,
source: function( request, response ) {
var matcher = new RegExp( request.term, "i" );
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
response( select.children( "option" ).map(function() {
var text = $( this ).text();
if ( this.value && ( !request.term || matcher.test(text) ) )
Expand All @@ -54,10 +54,20 @@
},
change: function( event, ui ) {
if ( !ui.item ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
return false;
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( this.value.match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this ).val( "" );
select.val( "" );
return false;
}
}
}
})
Expand Down

0 comments on commit 5d1e297

Please sign in to comment.