错误提示太生硬,怎么优化用户体验?

皇甫彬丽 阅读 15

我现在的表单校验一出错就直接弹 alert,用户说体验很差,但我不知道该怎么改才更友好。

试过用 document.getElementById('error').innerText = '请输入邮箱' 显示在输入框下面,但样式丑得不行,而且多个错误会互相覆盖。

有没有一套简单又好看的错误提示方案?比如像 Element Plus 那种带红色边框和小图标的效果?

我来解答 赞 2 收藏
二维码
手机扫码查看
2 条解答
a'ゞ熙研
简单,直接给你写一套轻量级的方案,不用引入 Element Plus 也能达到类似效果。

核心思路是:给每个输入框包裹一层容器,错误时给容器加个 class,CSS 负责控制样式。

HTML 结构这样改:

<div class="form-item">
<input type="text" id="email" placeholder="请输入邮箱">
<span class="error-message"></span>
</div>

<div class="form-item">
<input type="text" id="username" placeholder="请输入用户名">
<span class="error-message"></span>
</div>


CSS 样式:

.form-item {
margin-bottom: 20px;
position: relative;
}

.form-item input {
padding: 10px 12px;
border: 1px solid #dcdfe6;
border-radius: 4px;
width: 200px;
transition: border-color 0.2s;
}

.form-item input:focus {
border-color: #409eff;
outline: none;
}

/* 错误状态 */
.form-item.has-error input {
border-color: #f56c6c;
background: #fef0f0;
}

/* 错误图标 */
.form-item.has-error input {
padding-left: 32px;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='%23f56c6c'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3Cline x1='12' y1='8' x2='12' y2='12' stroke='white' stroke-width='2'/%3E%3Ccircle cx='12' cy='16' r='1' fill='white'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: 10px center;
}

.error-message {
display: block;
color: #f56c6c;
font-size: 12px;
margin-top: 6px;
min-height: 18px;
}


JS 校验逻辑:

function showError(inputId, message) {
const item = document.getElementById(inputId).parentElement;
const errorSpan = item.querySelector('.error-message');

item.classList.add('has-error');
errorSpan.textContent = message;
}

function clearError(inputId) {
const item = document.getElementById(inputId).parentElement;
const errorSpan = item.querySelector('.error-message');

item.classList.remove('has-error');
errorSpan.textContent = '';
}

// 校验示例
function validate() {
const email = document.getElementById('email').value;

if (!email) {
showError('email', '请输入邮箱');
return false;
}
if (!/^S+@S+.S+$/.test(email)) {
showError('email', '邮箱格式不正确');
return false;
}

clearError('email');
return true;
}

// 输入时自动清除错误
document.getElementById('email').addEventListener('input', function() {
clearError('email');
});


这样每个输入框的错误独立显示,互不覆盖。红色边框+图标+底部文字,Element Plus 差不多就这意思。

如果你想更懒一点,直接用现成的表单验证库比如 Parsley.js 或者 VeeValidate 也行,但上面这套自己可控,样式想怎么调都行。
点赞
2026-03-13 12:17
W″海宇
哈这个问题我之前也碰到过,alert确实太粗暴了,用户会被吓一跳。我给你个简单实用的方案,用CSS+JS就能搞定:

首先在输入框旁边加个错误提示区域:




然后写点CSS让它看起来专业点:
.error-msg {
color: #f56c6c;
font-size: 12px;
margin-top: 4px;
display: none; /* 默认隐藏 */
}
.error-input {
border-color: #f56c6c !important;
}


JS部分这样处理校验:
function validateEmail() {
const email = document.getElementById('email')
const errorMsg = email.nextElementSibling

if(!email.value.includes('@')) {
email.classList.add('error-input')
errorMsg.textContent = '请输入有效的邮箱地址'
errorMsg.style.display = 'block'
return false
} else {
email.classList.remove('error-input')
errorMsg.style.display = 'none'
return true
}
}


几个小技巧:
1. 错误提示用div而不是alert,位置固定在输入框下方
2. 输入框错误时加个红色边框(记得用!important覆盖原有样式)
3. 多个错误时可以用不同的error-msg元素,不会互相覆盖
4. 可以加个小图标,用before伪元素或者直接放个svg

这个方案比Element Plus简陋点,但核心效果都有了。真要追求完美可以去GitHub找现成的校验库,不过自己写更有成就感不是吗?
点赞
2026-03-08 22:29