1
我一直在嘗試創建自己的自定義LiveValidation(LiveValidation.com)函數,該函數連接到數據庫並檢查用戶名是否已存在。這是形式的相關部分:自定義實時驗證功能
Username: <input type="text" name="username" id="username" class="textinput">
<script type="text/javascript">
var username = new LiveValidation('username');
username.add(Validate.Presence);
username.add(Validate.Length, { minimum: 3, maximum: 12 });
username.add(Validate.Username);
</script>
這是我Validate.Username功能:
Validate.Username = function(value, paramsObj){
var paramsObj = paramsObj || {};
var message = paramsObj.failureMessage || "Sorry that username is taken!";
var http = new XMLHttpRequest();
var url = "usernamecheck.php";
var params = "username="+value;
http.open("POST", url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
if(http.responseText == 'yes') {
return true;
} else {
Validate.fail(message);
return false;
}
}
}
http.send(params);
}
這裏的usernamecheck.php的相關部分:
if(mysql_num_rows($query) != 0 && $query) {
echo "no";
} else if ($query) {
echo "yes";
} else {
echo "err";
}
的功能似乎工作正常(如果我把alert('fail')
以上Validate.fail(message);
我使用使用的用戶名時收到警報),但LiveValidation從不顯示錯誤消息。任何人都知道我做錯了什麼?
感謝