Javascript Autocomplete email's domain using jQuery UI -
i need help, stuck trying make following case scenario work: have email input field, type: foo@y - should pop autocomplete box, offering yahoo.com (for example). if take suggestion, end value should become: foo@yahoo.com
i have wrote code (modified off jquery ui sample):
$( "#tags" ) // don't navigate away field on tab when selecting item .bind( "keydown", function( event ) { if ( event.keycode === $.ui.keycode.tab && $( ).data( "autocomplete" ).menu.active ) { event.preventdefault(); } }) .autocomplete({ minlength: 3, source: function( request, response ) { var mail_regex = /^([\w.]+)@([\w.]+)$/; var match = mail_regex.exec(request.term); if (match) var matcher = new regexp( "^" + match[2], "i" ); response( $.grep( availabletags, function( item ){ return matcher.test( item ); }) ); }, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = split( this.value ); // remove current input terms.pop(); // add selected item terms.push( ui.item.value ); // add placeholder comma-and-space @ end terms.push( "" ); this.value = terms.join( ", " ); return false; } });
full working interactive sample: http://jsfiddle.net/rrf2s/3/
however, replaces foo@ yahoo.com - can not life of me figure out how override behaviour...
any javascript/jquery masters - please! how accomplish goal? tried doing: return match[1]+matcher.test( item ), not work.
here complete code:
$(function() { var availabletags = [ "yahoo.com", "gmail.com" ]; function extractlast( val ) { if (val.indexof("@")!=-1){ var tmp=val.split("@"); console.log(tmp[tmp.length-1]); return tmp[tmp.length-1]; } console.log("returning empty"); return ""; } $( "#tags" ) // don't navigate away field on tab when selecting item .bind( "keydown", function( event ) { if ( event.keycode === $.ui.keycode.tab && $( ).data( "autocomplete" ).menu.active ) { event.preventdefault(); } }) .autocomplete({ minlength: 1, source: function( request, response ) { var mail = extractlast(request.term); if(mail.length<1){return;} var matcher = new regexp( "^" + mail, "i" ); response( $.grep( availabletags, function( item ){ return matcher.test( item ); })); }, focus: function() { // prevent value inserted on focus return false; }, select: function( event, ui ) { var terms = this.value.split(", "); // remove current input var ml=terms[terms.length-1].split("@")[0]; terms.pop(); // add selected item terms.push( ml+"@"+ui.item.value ); // add placeholder comma-and-space @ end terms.push( "" ); this.value = terms.join( ", " ); return false; } }); });
Comments
Post a Comment