Jump to content

Crazy and odd, take a guess at this javascript issue.


jimmyt1988

Recommended Posts

registration.validate() is called.

 

 

var registration = {

    flushErrors: function(){

        $('form[name=registrationForm] .error').html("");

        this.errorCollection = [];

        this.errorMessage = "";

        this.validationCollection = [];

        this.validationErrorMessage = "";

        this.progress = true;       

    },

    sanitiseField: /([A-Za-z0-9\_\-\.])$/,

    usernameMinimumChar: 5,

    passwordMinimumChar: 6,   

    errorColour: "#F46C38",

    errorNotificationType: "border-color",

    errorCollection: [],

    errorMessage: "",

    errorPrefix: "Please fill in the following missing fields: ", 

    validationCollection: [],

    validationErrorMessage: "",

    validatedColour: "#F0F0F0",

    progress: true,

    ajaxCall: function(process, obj, type){

        $.post("registration-post.php", type + "=" + process + "&check=1",

            function(data){

                var returnedValue = $.trim(data);

                if(returnedValue==""){

                    $(obj).css(registration.errorNotificationType, registration.validatedColour);

                }else{

                    //1

                    alert(returnedValue);

                    registration.validationCollection.push(String(returnedValue));

                    //$('.error').append(returnedValue + "<br />");

                    $(obj).css(registration.errorNotificationType, registration.errorColour);

                    registration.progress = false;   

                }

            }

        );

    },

    emailValidation: function(email, obj, type){

        var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

        if(!filter.test(email)){

            this.validationCollection.push("The email address you have entered does not seem to be a valid entry");

            $(obj).css(registration.errorNotificationType, registration.errorColour);

            this.progress = false;         

        }else{

            $(obj).css(registration.errorNotificationType, registration.validatedColour);         

        } 

       

        this.ajaxCall(email, obj, type);

    }, 

    usernameValidation: function(username, obj, type){

        if(!this.sanitiseField.test(username)){

            this.validationCollection.push("Your username must only contain (A-Z, a-z, 0-9, _, -, .). Please try again.");

            $(obj).css(registration.errorNotificationType, registration.errorColour);

            this.progress = false;       

        }else{

            $(obj).css(registration.errorNotificationType, registration.validatedColour);

        }

       

        if(username.length < this.usernameMinimumChar){

            this.validationCollection.push("Your username must contain atleast " + this.usernameMinimumChar + "  characters");

            $(obj).css(registration.errorNotificationType, registration.errorColour);

            this.progress = false;     

        }else{

            $(obj).css(registration.errorNotificationType, registration.validatedColour);

        } 

        this.ajaxCall(username, obj, type);

    },

    passwordValidation: function(password, obj){       

        if(!this.sanitiseField.test(password)){

            this.validationCollection.push("Your password must only contain (A-Z, a-z, 0-9, _, -, .). Please try again.");

            $(obj).css(registration.errorNotificationType, registration.errorColour);

            this.progress = false;       

        }else{

            $(obj).css(registration.errorNotificationType, registration.validatedColour);

        }

           

        if(password.length < this.passwordMinimumChar){

            this.validationCollection.push("Your password must contain atleast " + this.passwordMinimumChar + " characters");

            $(obj).css(registration.errorNotificationType, registration.errorColour);

            this.progress = false;     

        }else{

            $(obj).css(registration.errorNotificationType, registration.validatedColour);

           

            if(password!=$('#registerUser input[name=confirmPassword]').val()){

                this.validationCollection.push("Your confirmation password does not match your chosen password");

                $($('#registerUser input[name=confirmPassword]').val()).css(registration.errorNotificationType, registration.errorColour);

                this.progress = false; 

            }else{

                $($('#registerUser input[name=confirmPassword]').val()).css(registration.errorNotificationType, registration.validatedColour);

            }

        }           

    },

    validate: function(){

        this.flushErrors();

       

        $('#registerUser input[type=text], #registerUser input[type=password]').each(

            function(){

                if($(this).val() == ""){

                    $(this).css(registration.errorNotificationType, registration.errorColour);

                    //registration.errorCollection.push($(this).parent().prev().text().replace(": ", ""));

                    registration.errorCollection.push($(this).parent().prev().text());

                    registration.progress = false;

                }else{

                    switch($(this).attr("name")){

                        case "email":

                            registration.emailValidation($(this).val(), $(this), $(this).attr("name"));

                        break;

                        case "username":

                            registration.usernameValidation($(this).val(), $(this), $(this).attr("name"));           

                        break;

                        case "password":

                            registration.passwordValidation($(this).val(), $(this));

                        break;

                        default:

                            $(this).css(registration.errorNotificationType, registration.validatedColour);

                    }

                }                                       

            }

        );

        if(this.progress){

            return this.progress;

        }else{   

            for (var i=0; i<this.errorCollection.length; i++){

                if(i==(this.errorCollection.length)-1){

                    this.errorMessage += this.errorCollection;

                }else{

                    this.errorMessage += this.errorCollection + ", ";

                }

            }

            //2

            alert("");

            for (var i=0; i<this.validationCollection.length;i++){

                if(i==(this.validationCollection.length)-1){

                    this.validationErrorMessage += this.validationCollection;

                }else{

                    this.validationErrorMessage += this.validationCollection + "<br />";

                }

            }       

            if(this.errorMessage == ""){

                $('form[name=registrationForm] .error').html(this.validationErrorMessage);

            }else{

                $('form[name=registrationForm] .error').html(this.errorPrefix + this.errorMessage + "<br />" + this.validationErrorMessage);

            }

        }       

           

        return this.progress;         

    }   

}

 

 

1st red highlighted code: the alert returns "The email you have entered is already registered. Please try another!". I push it into an array...

2nd red highlighted code: If i do not call an alert just before itterating through my array, the array contains nothing.. If I itterate through it just after an alert(""); the array works fine.

 

explain :D

Link to comment
Share on other sites

I suspect it is because your AJAX call is asynchronous? Not sure since I'm not going to try and decypher a 100 lines of code to understand what is calling what. The fact that you are putting in an alert gives enough time for the asynchonous process to complete before that block of code is executed.

Link to comment
Share on other sites

YEHAAAAAAA WOOPEEEEE WIPPEEEEEe, YIPPEEE DOO DAAAAAA, YIPPEEEDEE DAYYY...

 


$.ajax({
            type: "POST",
            url: "registration-post.php",
            data: type + "=" + process + "&check=2",
            async: false,
            success: function(data){
                var returnedValue = $.trim(data);
                if(returnedValue=="ok"){
                    $(obj).css(registration.errorNotificationType, registration.validatedColour);
                }else{
                    registration.validationCollection.push(returnedValue);
                    //$('.error').append(returnedValue + "<br />");
                    $(obj).css(registration.errorNotificationType, registration.errorColour);
                    registration.progress = false;  
                    alert(registration.progress);                       
                }
            }
        });

 

async: false LOLLLLLL!!!!

 

OMG everything just works perfect.. YEHAAAAAA, finit!

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.