首先,出于安全考虑,你不愿承认的用户名是正确的事 - 消息应该是比较通用的,如“请输入有效的用户名和密码”
的另一个考虑因素是凭据如何验证。这可以通过一个返回JSON结果的简单web服务来完成。所以一个请求:/verifycreds.php?username=John&password=password
可以返回以下内容:
{ success: false, message: "Please enter a valid username and password" }
从那里的形式将与onsubmit处理更新提交页面之前验证凭据。在这个例子中,我将使用jQuery - please reference the documentation
$("#myform").submit(function(event) {
var verificationURL = "/verifycreds.php?username="
+ $('#username').val()
+ "&password=" + $('#password').val();
$.get(verificationURL, function(data) {
if(!data.success) {
event.preventDefault();
// Display popup of your choice here with data.message
}
});
});
这是基本设置。我们正在向#myForm
添加一个onSubmit处理程序,我们构建验证凭证webservice的URL,然后处理结果并在验证失败并显示消息时停止表单提交。