Why is this ajax call being made even though it shouldn't be

Posted by user2921557 on Stack Overflow See other posts from Stack Overflow or by user2921557
Published on 2013-10-30T21:48:00Z Indexed on 2013/10/30 21:53 UTC
Read the original article Hit count: 172

Filed under:
|
|
|
|

i have this validation script im working on but cant see why im having an issue.

You can see that i have a check = false / true, check before it runs the ajax call.

However, even if a field is empty and check is set to false, it is still running the call.

so:

// JavaScript - Update Password AJAX

$(document).ready(function () {

    // When the form is submitted
    $('.updatepasswordform').submit(function () {

        var check = true;

        // Get the values
        var password1 = $("input[name=password1]").val();
        var password2 = $("input[name=password2]").val();
        var newpassword = $("input[name=newpassword]").val();

        /* Password Validation */

        // If fields are empty
        if (password1 === '') {
            check = false;
            $("input[name=password1]").css('border', 'solid 2px red');
        }

        // If fields are empty
        if (password2 === '') {
            check = false;
            $("input[name=password2]").css('border', 'solid 2px red');
        }

        // If fields are empty
        if (newpassword === '') {
            check = false;
            $("input[name=newpassword]").css('border', 'solid 2px red');
        }

        if (check = true) {
            $.ajax({
                type: "POST",
                url: "process/updatepassword.php",
                data: $(".updatepasswordform").serialize(),
                dataType: "json",
                success: function (response) {

                    /* Checks for database validation, removed for space saving */

                }

            });
        }
        return false;
    });
});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery