You could try appending what is being typed in to the list of suggestions. That way they can essentially select what they are typing using "req.term". Like this:
//process response
$.each(data, function(i, val){
suggestions.push(val.name);
});
//append what has been typed in so it's available for selection
suggestions.push(req.term);
//pass array to callback
add(suggestions);
Then, in the select: function, you could insert the selection into the database with an ajax call if it doesn't already exists.
//define select handler
select: function(e, ui) {
//create formatted friend
var friend = ui.item.value,
span = $("<span>").text(friend),
a = $("<a>").addClass("remove").attr({
href: "javascript:",
title: "Remove " + friend
}).text("x").appendTo(span);
//add friend to friend div
span.insertBefore("#to");
//insert selected email if doesn't already exists
},
Here's a keypress example for inserting you formated friend on enter:
$("#to").keypress(function(e){
var code = (e.keyCode ? e.keyCode : e.which);
if (code == 13) { //Enter keycode
e.preventDefault();
//create formatted friend
var friend = $(this).val(),
span = $("<span>").text(friend),
a = $("<a>").addClass("remove").attr({
href: "javascript:",
title: "Remove " + friend
}).text("x").appendTo(span);
//add friend to friend div
span.insertBefore("#to");
$(this).autocomplete('close');
}
});