首先,出於安全考慮,你不願承認的用戶名是正確的事 - 消息應該是比較通用的,如「請輸入有效的用戶名和密碼」
的另一個考慮因素是憑據如何驗證。這可以通過一個返回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,然後處理結果並在驗證失敗並顯示消息時停止表單提交。