Skip to content

Commit 12b8a29

Browse files
committed
Max tag length limiting
1 parent 91039a5 commit 12b8a29

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

example/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414

1515
/* Initialize TagTrain */
1616
var tagTrain = new TagTrain({
17-
maxTags: 5
17+
maxTags: 5,
18+
maxTagLen: 16
1819
});
1920

2021
tagTrain.setTags(['foo', 'bar']);

src/tagtrain.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function TagTrain(opts) {
4141

4242
this.input = document.createElement('input');
4343
this.input.classList.add(TagTrain.classNames.input);
44+
if (this.opts.maxTagLen !== Infinity) {
45+
this.input.maxLength = this.opts.maxTagLen;
46+
}
4447
this.el.appendChild(this.input);
4548

4649
this.events = {
@@ -72,6 +75,7 @@ TagTrain.defaultOpts = {
7275
delimiters: [9, 13, 32, 188],
7376
removeLast: 8,
7477
invalidTag: /\W/,
78+
maxTagLen: Infinity,
7579
maxTags: Infinity
7680
};
7781

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

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

0 commit comments

Comments
 (0)