Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool jQuery addMethod

    Hi!

    So I am using jQuery validate to validate my form.

    I have added a custom rule to it that checks for username availiability.

    The code is:

    Code:
    <script type="text/javascript" charset="utf-8">
    $.validator.addMethod("uniqueUserName", function(value, element) {
    $.ajax({
       type: "POST",
    	  url: "../wp-content/themes/freshserve/check.php",
    	  data: "username="+value,
    	  dataType: "html",
              success: function(msg){
    		$("#mike").html(msg);
    		return msg;
             }
     });
    });
    
    $("#signup_form").validate({
        debug: true, //prevent submission for testing
        rules: {
            username: {
                uniqueUserName: true
            }
        },
        messages: {
            username: {
                uniqueUserName: "Username Taken"
            }
        }
    }); 
    </script>
    It updates the div id mike to be true or false which is what I return from the php script but even if the username is available is still displays the message 'Username Taken'.

    How to stop this happening if the username is availiable?

    Thanks!

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    What I'd do is:

    1. Try swapping:
    return msg;
    for
    return true;
    and
    return false;
    just to confirm that returning true or false is making the validation script behave as required.

    2. If that's confirmed, then your AJAX call isn't returning what you expect it to. I'd guess that's its returning the string "false" which, if I'm not mistaken, can be interpreted as true. Try instead:

    return (msg.toLowerCase() == 'true') ? true : false;

    which will return an actual boolean value.

  • #3
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply.

    I tried changing it to:

    Code:
    $.validator.addMethod("uniqueUserName", function(value, element) {
    $.ajax({
       type: "POST",
    	  url: "../wp-content/themes/freshserve/check.php",
    	  data: "username="+value,
    	 dataType: "html",
            success: function(msg){
    		$("#mike").html(msg);
    		return (msg.toLowerCase() == 'true') ? true : false;
       }
     });
    });
    
    $("#signup_form").validate({
        debug: true, //prevent submission for testing
        rules: {
            username: {
                uniqueUserName: true
            }
        },
        messages: {
            username: {
                uniqueUserName: "Username Taken"
            }
        }
    });
    and then
    Code:
    $.validator.addMethod("uniqueUserName", function(value, element) {
    $.ajax({
       type: "POST",
    	  url: "../wp-content/themes/freshserve/check.php",
    	  data: "username="+value,
    	 dataType: "html",
            success: function(msg){
    		$("#mike").html(msg);
    		return true;
       }
     });
    });
    
    $("#signup_form").validate({
        debug: true, //prevent submission for testing
        rules: {
            username: {
                uniqueUserName: true
            }
        },
        messages: {
            username: {
                uniqueUserName: "Username Taken"
            }
        }
    });
    neither worked...

    my php code is:

    Code:
    if(isSet($_POST['username']))
    {
    $username = strtolower($_POST['username']);
    
    //connect to database  
    require_once("database_date.php");
    
    $sql_check = mysql_query('SELECT username FROM tbl_users WHERE MD5(username) ="'. strtolower(md5($username)) .'"');
    
    if(mysql_num_rows($sql_check) > 0)
    {
    echo 'true';
    }else{
    echo 'false';
    }
    }

  • #4
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Doh. My bad.

    You're doing an AJAX request, which is asynchronous by default. This means that your validation function is returning before the AJAX request has returned a response.

    You can do a synchronous request by setting it in your AJAX params. See this near-identical post.

  • #5
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks again.

    Still not working though.

    Here is my current code:

    Code:
    <script type="text/javascript" charset="utf-8">
    $.validator.addMethod("uniqueUserName", function myValidator(value, element) {
       var isSuccess = false;
    
       $.ajax({ 
    	  url: "../wp-content/themes/freshserve/check.php",
    	  data: "username="+value,
                async: false, 
    												datatype: "html",
                success: 
                    function(msg) { isSuccess = msg === "true" ? true : false }
              });
        return isSuccess;
    });
    
    
    $("#signup_form").validate({
        debug: true, //prevent submission for testing
        rules: {
            username: {
                uniqueUserName: true
            }
        },
        messages: {
            username: {
                uniqueUserName: "Username Taken"
            }
        }
    }); 
    
    </script>

  • #6
    New to the CF scene
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, since adding

    async: false,

    it's slowed the browser down quite a lot so while typing a username it doesn't always register every key press


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •