开发者

Custom Live Validation Function

开发者 https://www.devze.com 2022-12-14 14:51 出处:网络
I\'ve been trying to create my own custom LiveValidation (LiveValidation.com) function that connects to a database and checks if a username already exists. This is the relevant part of the form:

I've been trying to create my own custom LiveValidation (LiveValidation.com) function that connects to a database and checks if a username already exists. This is the relevant part of the form:

    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>

This is my Validate.Username function:

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);
}

Here's the relevant part of usernamecheck.php:

if(mysql_num_rows($query) != 0 && $query) {
            echo "no";
        } else if ($query) {
            echo "yes";
        } else {
            echo "err";
        }

The function seems to work fine (if I put alert('fail') above Validate.fail(message); I get an alert when a used username is used), but LiveValidation never shows the error message. Anyone know what I've done wrong?

Thanks


Hmmm... just looking at it real quick, could it be that "message" is not defined in your function ? It is defined in the parent function.

0

精彩评论

暂无评论...
验证码 换一张
取 消