Skip to content

Commit

Permalink
Max tag length limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
rvighne committed Aug 17, 2016
1 parent 91039a5 commit 12b8a29
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

/* Initialize TagTrain */
var tagTrain = new TagTrain({
maxTags: 5
maxTags: 5,
maxTagLen: 16
});

tagTrain.setTags(['foo', 'bar']);
Expand Down
6 changes: 5 additions & 1 deletion src/tagtrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ function TagTrain(opts) {

this.input = document.createElement('input');
this.input.classList.add(TagTrain.classNames.input);
if (this.opts.maxTagLen !== Infinity) {
this.input.maxLength = this.opts.maxTagLen;
}
this.el.appendChild(this.input);

this.events = {
Expand Down Expand Up @@ -72,6 +75,7 @@ TagTrain.defaultOpts = {
delimiters: [9, 13, 32, 188],
removeLast: 8,
invalidTag: /\W/,
maxTagLen: Infinity,
maxTags: Infinity
};

Expand All @@ -98,7 +102,7 @@ TagTrain.prototype.trigger = function trigger(event) {
};

TagTrain.prototype.addTag = function addTag(value) {
if (value !== '' && this.tags.length < this.opts.maxTags && !this.opts.invalidTag.test(value) && this.tags.indexOf(value) === -1) {
if (value !== '' && this.tags.length < this.opts.maxTags && value.length <= this.opts.maxTagLen && !this.opts.invalidTag.test(value) && this.tags.indexOf(value) === -1) {
var item = document.createElement('li');
item.classList.add(TagTrain.classNames.tagItem);
item.id = TagTrain.tagIdPrefix + value;
Expand Down

0 comments on commit 12b8a29

Please sign in to comment.