我一直在尝试创建自己的自定义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从不显示错误消息。谁知道我做错了什么?
由于
答案 0 :(得分:1)
嗯...只是快速地看着它,可能是你的功能中没有定义“消息”吗?它在父函数中定义。