]> source.dussan.org Git - jquery-ui.git/commitdiff
Autocomplete (combobox demo): Detect valid entries when typed, but not selected from...
authorScott González <scott.gonzalez@gmail.com>
Tue, 20 Jul 2010 18:35:54 +0000 (14:35 -0400)
committerScott González <scott.gonzalez@gmail.com>
Tue, 20 Jul 2010 18:35:54 +0000 (14:35 -0400)
demos/autocomplete/combobox.html

index 37b74e155d4b9b3c5d979bd865bc4b294c593271..c76cfa1305b4513c80b18f74051230ffbe176b71 100644 (file)
@@ -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) ) )
                                                },
                                                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;
+                                                               }
                                                        }
                                                }
                                        })