开发者

Jquery Validate always returns false on email validation even with true being returned

I know there have been posts that are almost the same thing I have looked through alot of them and sadly have not found a solution. My problem is that I wrote a email valiadtor using jquery's .addmethod() function to the jquery validation plugin.

$.validator.addMethod("uniqueEmail", function(value, element) {
    $.ajax({
        data: 'email=' + value,
        type: 'get',
        url: 'functions/emailcheck.php',
            dataType:"html",
            success: function(msg) {
                alert("M:"+msg+"   V:"+value);
                if(msg == true || msg != null || msg == "true"){
                    return true;
                    alert("TRUE!!!");
                }
                else{
                    return false;
                }
            }
        });
    });

Then in the validation rules...

email: { required: true, email: true, uniqueEmail: true },

Then finally even during with this php file....

<?php
    echo "true";
?>

It gives me the warning true has already been registered , with the message under email.. uniqueEmail: jQuery.format("{0} has already been registered"). Now I have a debugging alert set int he ajax call. it shows that it returns true, and the value is as entered.

I hope for some help this is driving me nuts and I have a feeling it'll be obvious to some of you!

PS heres my full Validation minus the above additional method...

$( ".form_registration_submit" ).click(function() {
        $(".qtip").remove();
        $("#form_registration").validate({
            onfocusout: function(element){
                $(".qtip").remove();
            },
            rules: {
                name: {
                    required: true,
                    minlength: 3
                },
                firstname: {
                    required: true,
                    minlength: 3
                },
                lastname: {
                    required: true,
                    minlength: 3
                },
                username: {
                    required: true,
                    minlength: 3
                },
                password: {
                    required: true,
                    minlength: 5
                },
     开发者_StackOverflow           confirm_password: {
                    required: true,
                    minlength: 5,
                    equalTo: "#form_registration_password"
                },
                email: {
                    required: true,
                    email: true,
                    uniqueEmail: true
                },
                topic: {
                    required: "#newsletter:checked",
                    minlength: 2
                },
                agree: "required"
            },
            messages: {
                name: "Please enter your name",
                firstname: "Please enter your firstname",
                lastname: "Please enter your lastname",
                username: {
                    required: "Please enter a username",
                    minlength: "Your username must consist of at least 2 characters"
                },
                password: {
                    required: "Please provide a password",
                    minlength: "Your password must be at least 5 characters long"
                },
                confirm_password: {
                    required: "Please confirm your password",
                    minlength: "Your password must be at least 5 characters long",
                    equalTo: "Please enter the same password as above"
                },
                email: {
                    required: "Please enter a valid email address",
                    uniqueEmail: jQuery.format("{0} has already been registered")
                },
                agree: "Please accept our policy"
            },
             errorPlacement: function(error, element) {
              element.qtip(
                  {
                      show: { solo: false, ready: true, delay: 0 },
                      hide: { when:false },
                      content: eval(error),
                      style: { name: 'dark', border: { width: 1, radius: 1, color:"red" }, tip: 'bottomLeft' },
                      position: {
                          corner: { target: 'topRight', tooltip: 'bottomLeft' },
                          adjust: { x: 0 , y: 0 }
                      }
                  });
            },
            submitHandler: function(form) {
                $(".qtip").remove();
                jQuery(form).ajaxSubmit({
                    target: "#login_result"
                });
            }
        }); 
    });


Your validation function doesn't actually return any value, hence evaluating to false. By default .ajax() requests are asynchronous, meaning that the validation function ends before there is a chance of executing your success callback.

This issue is a duplicate of .post inside jQuery.validator.addMethod always returns false


Maybe your validator code is inside a click function (or other event), so it's called many times? You should put it once or inside a $(document).ready(). Also, you should send data as this:

...
data: {'email': value},
...

so it will be urlencoded. If you don't put it, emails such as 'a@a.c&om' will be valid


Just add async: false parameter for your ajax call and you'll be good to go

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜