]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete: Validate input when you close by menu by clicking. Fixed #7197 - Combob... 225/head
authorKato Kazuyoshi <kato.kazuyoshi@gmail.com>
Fri, 6 May 2011 23:40:38 +0000 (08:40 +0900)
committerKato Kazuyoshi <kato.kazuyoshi@gmail.com>
Fri, 6 May 2011 23:40:38 +0000 (08:40 +0900)
demos/autocomplete/combobox.html

index ded809e1e9a7e41846c6e36f04ffb3c2ee0e01da..00a60a85bbbacf0a0d643497dcad41334fe51579 100644 (file)
                                        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 )
                                                        });
                                                },
                                                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" );
                                                // close if already visible
                                                if ( input.autocomplete( "widget" ).is( ":visible" ) ) {
                                                        input.autocomplete( "close" );
+                                                       removeIfInvalid( input );
                                                        return;
                                                }