javascript - jQuery - Multiple Email Addresses in Input Field -


i working on form allow users enter multiple email addresses in input field.

by searching around net cam across jquery plugin: http://t2a.co/blog/index.php/multiple-value-input-field-with-jquery/

(function( $ ){       $.fn.multipleinput = function() {            return this.each(function() {                 // create html elements                 // list of email addresses unordered list                $list = $('<ul />');                 // input                var $input = $('<input type="text" />').keyup(function(event) {                      if(event.which == 32 || event.which == 188) {                          // key press space or comma                          var val = $(this).val().slice(0, -1); // remove space/comma value                           // append list of emails remove button                          $list.append($('<li class="multipleinput-email"><span> ' + val + '</span></li>')                               .append($('<a href="#" class="multipleinput-close" title="remove" />')                                    .click(function(e) {                                         $(this).parent().remove();                                         e.preventdefault();                                    })                               )                          );                          $(this).attr('placeholder', '');                          // empty input                          $(this).val('');                     }                 });                 // container div                var $container = $('<div class="multipleinput-container" />').click(function() {                     $input.focus();                });                 // insert elements dom                $container.append($list).append($input).insertafter($(this));                 // add onsubmit handler parent form copy emails original input csv before submitting                var $orig = $(this);                $(this).closest('form').submit(function(e) {                      var emails = new array();                     $('.multipleinput-email span').each(function() {                          emails.push($(this).html());                     });                     emails.push($input.val());                      $orig.val(emails.join());                 });                 return $(this).hide();            });       }; })( jquery ); 

i installed plugin, below screenshot of results:

enter image description here

my questions:

how can edit plugin make sure last value (email address) not have comma @ end?

here fiddle: https://jsfiddle.net/william3780/ol14dgqp/

and here link live install working on: http://51146.com/free/refer-a-friend/ *please fill out "friends' emails" field , submit form see issue.

your input appreciated, thank you.

jsfiddle appreciated if trying remove comma of last element of array or array of items in string. can imagine few approaches:

1) regex

var lastcommapatter = ",$";  var emails = "test@gmail.com, test2@gmail.com, test@gmail.com,";  email.replace(lastcommapatter, ""); 

2) coverting entire thing collection , remove last blank element

var separator = ",";  var emails = "test@gmail.com, test2@gmail.com, test@gmail.com,";  var emailscollection = email.split(separator);  var collectionsize = emailscollection.length;  if(collectionsize > 0){    array.splice(emailscollection, collectionsize - 1);  } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -