Jump to content

jQuery addmethod issue


Strychnine
Go to solution Solved by 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
Share on other sites

  • Solution
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('');
}
}
})
});
}
},

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.