diff --git a/src/components/Course/courseTag.vue b/src/components/Course/courseTag.vue index 2a126745..2d15ac5c 100644 --- a/src/components/Course/courseTag.vue +++ b/src/components/Course/courseTag.vue @@ -176,11 +176,22 @@ export default { //按回车键,创建新标签 handleEnterKey(event) { const inputVal = event.target.value?.trim() + if (!inputVal) return; + // 检查是否与已选择的标签重复 + const isDuplicate = this.selectedTags.some(tag => tag.tagName === inputVal); + if (isDuplicate) { + this.$message.warning('该标签已存在,无需重复创建'); + event.target.value = ''; + return; + } if (!this.searchResults.length && inputVal && this.selectedTags.length < this.maxTags) { this.createNewTag(event.target.value.trim()) event.target.value = '' } else if (this.selectedTags.length >= this.maxTags) { this.$message.warning('最多只能添加5个标签') + event.target.value = '' + } else { + event.target.value = '' } }, @@ -213,6 +224,18 @@ export default { this.$message.error('标签不能超过8个字') return; } + // 首先检查是否与已选择的标签重复 + const isDuplicate = this.selectedTags.some(tag => tag.tagName === tagName); + if (isDuplicate) { + this.$message.warning('该标签已存在,无需重复创建'); + return; + } + // 标签格式验证:仅支持中文、英文、数字、下划线、中横线 + const tagPattern = /^[\u4e00-\u9fa5a-zA-Z0-9_-]+$/; + if (!tagPattern.test(tagName)) { + this.$message.error('标签名称仅支持中文、英文、数字、下划线(_)和中横线(-),不支持空格、点和特殊字符'); + return; + } // 添加标签数量限制检查 if (this.selectedTags.length >= this.maxTags) { this.$message.warning('最多只能添加5个标签')