自定义 jquery.validate 方法总是显示无效

发布于 2024-12-05 21:16:08 字数 1641 浏览 0 评论 0原文

我有一个自定义验证方法来检查重复的用户名。对于 notDuplicateUsername,json 正确返回,但验证始终显示为无效。

    $('#register-form').validate({

    //see source of http://jquery.bassistance.de/validate/demo/ for example

    rules: {
        schoolname: {
            required: true
        },
        username: {
            required: true,
            notDuplicateUsername: true
        },
        password: {
            required: true
        },
        email: {
            required: true,
            email: true
        }

    },
    messages: {
        schoolname: 'Please tell us where you want to use Word Strip.',
        username: {
            required: 'Please choose a username.',
            notDuplicateUsername: 'Sorry, that username is already being used.'
        },
        password: 'Please choose a password.',
        email: 'Please can we have your email address.'

    }

});

jQuery.validator.addMethod(
    'notDuplicateUsername', 
    function(value, element, params){

        var toCheck = new Object();
        toCheck['username'] = $('#username').val();

        var data_string = $.toJSON(toCheck);//this is a method of the jquery.json plug in

        $.post('check_duplicate_username.php', {username_data: data_string}, function(result){
            var noDuplicate = true;

            var returned_data = $.evalJSON(result);//this is a method of the jquery.json plug in

            if (returned_data.status == 'duplicate'){
                noDuplicate = false;
            }

            console.log('value of noDuplicate: '+noDuplicate);
            return noDuplicate;
        });

    }
);

有人有任何线索吗?

I have a custom validation method that checks for duplicate usernames. The json returns correctly for notDuplicateUsername but the validation always shows up as invalid.

    $('#register-form').validate({

    //see source of http://jquery.bassistance.de/validate/demo/ for example

    rules: {
        schoolname: {
            required: true
        },
        username: {
            required: true,
            notDuplicateUsername: true
        },
        password: {
            required: true
        },
        email: {
            required: true,
            email: true
        }

    },
    messages: {
        schoolname: 'Please tell us where you want to use Word Strip.',
        username: {
            required: 'Please choose a username.',
            notDuplicateUsername: 'Sorry, that username is already being used.'
        },
        password: 'Please choose a password.',
        email: 'Please can we have your email address.'

    }

});

jQuery.validator.addMethod(
    'notDuplicateUsername', 
    function(value, element, params){

        var toCheck = new Object();
        toCheck['username'] = $('#username').val();

        var data_string = $.toJSON(toCheck);//this is a method of the jquery.json plug in

        $.post('check_duplicate_username.php', {username_data: data_string}, function(result){
            var noDuplicate = true;

            var returned_data = $.evalJSON(result);//this is a method of the jquery.json plug in

            if (returned_data.status == 'duplicate'){
                noDuplicate = false;
            }

            console.log('value of noDuplicate: '+noDuplicate);
            return noDuplicate;
        });

    }
);

Any clues anyone?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

李白 2024-12-12 21:16:08

也许您已经解决了这个问题,但以防万一。我遇到了类似的问题,发现我正在比较错误的类型。可能您的服务器将状态作为布尔值或其他数据类型发送,并且与字符串的比较失败并始终返回 false。

Probably you might have sorted this out already, but just in case. I faced a similar problem and found that I was comparing wrong types. May be your server sends the status as a boolean or some other datatype and its comparison to a string fails and always returns false.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文