< /code>
css < /p>
#tag-error {
/* Just making sure the div is visible and spaced */
color: red;
font-size: 14px;
min-height: 24px;
margin-top: 5px;
}
< /code>
js < /p>
console.log("
const input = document.querySelector('#tags');
const tagify = new Tagify(input, {
maxTags: 5,
enforceWhitelist: false,
dropdown: {
enabled: 1,
fuzzySearch: true,
position: 'auto',
highlightFirst: true
}
});
let wasDuplicate = false;
tagify.on('beforeAddTag', function(e) {
const newTag = e.detail.data.value.toLowerCase();
const existingTags = tagify.value.map(tag => tag.value.toLowerCase());
if (existingTags.includes(newTag)) {
e.preventDefault();
wasDuplicate = true;
console.log("
showError(`The tag "${newTag}" has already been added.`);
} else {
wasDuplicate = false;
console.log("
clearError();
}
});
tagify.on('add', function(e) {
const addedTag = e.detail.data.value.toLowerCase();
if (wasDuplicate) {
console.log("
wasDuplicate = false;
return;
}
console.log('
clearError();
});
tagify.on('invalid', function(e) {
const invalidTag = e.detail.data.value;
const reason = e.detail.message || "This tag is not valid";
console.log("
showError(reason);
});
//
function showError(msg) {
const errorEl = document.getElementById('tag-error');
if (errorEl) {
errorEl.innerText = msg;
errorEl.style.display = 'block';
errorEl.style.color = 'red';
errorEl.style.fontSize = '0.875rem';
errorEl.style.minHeight = '1.5rem';
console.log("
}
}
function clearError() {
const errorEl = document.getElementById('tag-error');
if (errorEl) {
errorEl.innerText = "";
errorEl.style.display = 'none';
console.log("
}
}
< /code>
Любая помощь, которую вы можете оказать, очень ценится.
Подробнее здесь: https://stackoverflow.com/questions/796 ... -incorrect
Мобильная версия