Skip to content

Commit

Permalink
Autocomplete: Validate input when you close by menu by clicking. Fixe…
Browse files Browse the repository at this point in the history
…d #7197 - Combobox: not valid value is set.
  • Loading branch information
kzys committed May 6, 2011
1 parent f7e8331 commit 9e26291
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions demos/autocomplete/combobox.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,32 @@
select = this.element.hide(),
selected = select.children( ":selected" ),
value = selected.val() ? selected.text() : "";

function removeIfInvalid(element) {
var value = $( element ).val(),
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( element )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}

var input = this.input = $( "<input>" )
.insertAfter( select )
.val( value )
Expand Down Expand Up @@ -57,30 +83,8 @@
});
},
change: function( event, ui ) {
if ( !ui.item ) {
var value = $( this ).val(),
matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( value ) + "$", "i" ),
valid = false;
select.children( "option" ).each(function() {
if ( $( this ).text().match( matcher ) ) {
this.selected = valid = true;
return false;
}
});
if ( !valid ) {
// remove invalid value, as it didn't match anything
$( this )
.val( "" )
.attr( "title", value + " didn't match any item" )
.tooltip( "open" );
select.val( "" );
setTimeout(function() {
input.tooltip( "close" ).attr( "title", "" );
}, 2500 );
input.data( "autocomplete" ).term = "";
return false;
}
}
if ( !ui.item )
return removeIfInvalid( this );
}
})
.addClass( "ui-widget ui-widget-content ui-corner-left" );
Expand Down Expand Up @@ -109,6 +113,7 @@
// close if already visible
if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
input.autocomplete( "close" );
removeIfInvalid( input );
return;
}

Expand Down

0 comments on commit 9e26291

Please sign in to comment.