Jump to content

jQuery addmethod issue


Strychnine

Recommended Posts

Hello all! I've written a jQuery addMethod for the jQuery validation plugin that seems to be causing an error.

 

Basically this method just checks the db to see if the email address is registered. It works correctly and prompts the error/or passes it correctly. However it fired back an error and sets the focus to the field on submit. If I take this out, the form fires correctly no problem. Any insight I would appreciate.

 

Submit Handler:

submitHandler: function(form) {
            if($("#registerForm").valid()) {
                $.ajax({
                    type: "POST",
                    async: false,
                    url: "activate.php",
                    data: $("#registerForm").serialize(),
                    success: function(data) {
                        $('#results').html(data);
                        $('#registerForm').fadeOut(1000);
                        $('#thanks').fadeToggle(1000);
                    }
                })
            }
        },

addMethod

$.validator.addMethod("checkEmailAvailability", function(email) {
        $('#email').blur(function() {
            $.ajax({
                type: "POST",
                url: "http://"+location.host+"/includes/checkemail.php?",
                data: "email=" +$("#email").val(),
                success: function(data) {
                    if(data == 'false') {
                        //$('#checkEmail').removeClass("validate-ok");
                        $('#checkEmail').addClass("validate-error").html('✗ ' + $("#email").val() + ' is already a registered email.');
                    }
                    else {
                        $('#checkEmail').html('');
                    }
                }
            })
        });
    });
Link to comment
https://forums.phpfreaks.com/topic/282840-jquery-addmethod-issue/
Share on other sites

Seems I figured out what the issues was. I wanted to maintain the same functionality as my method, but as most people say to use the remote method. With that I decided to wrap the remote with a function() that would process the same way, here is my updated code is anyone else has this issue: 

 



email: {
email: true,
required: true,
minlength: 8,
remote: function() {
$('#email').blur(function() {
$.ajax({
type: "POST",
url: "http://"+location.host+"/includes/checkemail.php?",
data: "email=" +$("#email").val(),
success: function(data) {
if(data == 'false') {
$('#checkEmail').addClass("validate-error").html('✗ ' + $("#email").val() + ' is already a registered email.');
}
else {
$('#checkEmail').html('');
}
}
})
});
}
},

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.