Skip to content

Commit 5d1e297

Browse files
committed
Autocomplete (combobox demo): Detect valid entries when typed, but not selected from menu. Fixes #5605 - Autocomplete combobox demo does not accept valid values.
1 parent ba09165 commit 5d1e297

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

demos/autocomplete/combobox.html

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
delay: 0,
3030
minLength: 0,
3131
source: function( request, response ) {
32-
var matcher = new RegExp( request.term, "i" );
32+
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term), "i" );
3333
response( select.children( "option" ).map(function() {
3434
var text = $( this ).text();
3535
if ( this.value && ( !request.term || matcher.test(text) ) )
@@ -54,10 +54,20 @@
5454
},
5555
change: function( event, ui ) {
5656
if ( !ui.item ) {
57-
// remove invalid value, as it didn't match anything
58-
$( this ).val( "" );
59-
select.val( "" );
60-
return false;
57+
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( $(this).val() ) + "$", "i" ),
58+
valid = false;
59+
select.children( "option" ).each(function() {
60+
if ( this.value.match( matcher ) ) {
61+
this.selected = valid = true;
62+
return false;
63+
}
64+
});
65+
if ( !valid ) {
66+
// remove invalid value, as it didn't match anything
67+
$( this ).val( "" );
68+
select.val( "" );
69+
return false;
70+
}
6171
}
6272
}
6373
})

0 commit comments

Comments
 (0)