From 12b8a29b23d2e14ab335810796107b1e24c1adcc Mon Sep 17 00:00:00 2001 From: Rohit Vighne Date: Tue, 16 Aug 2016 18:36:01 -0700 Subject: [PATCH] Max tag length limiting --- example/index.html | 3 ++- src/tagtrain.js | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/example/index.html b/example/index.html index 65d864a..4002f0f 100644 --- a/example/index.html +++ b/example/index.html @@ -14,7 +14,8 @@ /* Initialize TagTrain */ var tagTrain = new TagTrain({ - maxTags: 5 + maxTags: 5, + maxTagLen: 16 }); tagTrain.setTags(['foo', 'bar']); diff --git a/src/tagtrain.js b/src/tagtrain.js index 34992f8..5bf693b 100644 --- a/src/tagtrain.js +++ b/src/tagtrain.js @@ -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 = { @@ -72,6 +75,7 @@ TagTrain.defaultOpts = { delimiters: [9, 13, 32, 188], removeLast: 8, invalidTag: /\W/, + maxTagLen: Infinity, maxTags: Infinity }; @@ -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;