我正在处理注册表单。我已经验证了 required、minlength 等字段,并将验证错误消息放在这样的元素之前:
$('#register').validate(
{
rules:
{
username:
{
required: true,
minlength: 6
},
email:
{
required: true,
email: true
},
password:
{
required: true,
minlength: 8
},
confirmPassword:
{
required: true,
equalTo: '#password'
}
},
messages:
{
username:
{
required: 'Please enter username',
minlength: 'Username too short'
},
email:
{
required: 'Please enter email',
email: 'Invalid E-Mail'
},
password:
{
required: 'Please enter password',
minlength:'Password too short'
},
confirmPassword:
{
required: 'Please provide password',
equalTo: 'Passwords don\'t match'
}
},
errorPlacement: function(error, element)
{
error.insertBefore(element);
}
});
客户端验证时一切都很好,但我也必须检查服务器端验证,比如用户名重复、电子邮件重复......所以如果用户名已经存在,我必须再次在元素之前放置验证错误消息,那会怎么样呢?我已经完成了所有 ajax、请求和 php/mysql 编码并返回错误消息。只是我必须在与 jquery 验证具有相同样式的元素之前显示。