Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8613415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:52:12+00:00 2026-06-12T04:52:12+00:00

I have started learning the javascript module pattern and I have the following code:

  • 0

I have started learning the javascript module pattern and I have the following code:

// PersonalInformation.js

var PersonallInformation = (function () { 

   $.validator.addMethod("checkPhoneNumber", function (value, element) {

        if (!value) return true;
        return /^((\+7)|8)(700|701|702|705|707|712|713|717|718,721|725|726|727|777)[0-9]{7}$/.test(value);
    }, "Wrong phone format");

    function updateQTip() {

        $('div.invalid_form').qtip({
            content: function (api) {
                var text = $(this).prev();
                return "<div class='tip_cont'><span class='simple cost'><span class='corner'></span>" + $(text).attr('data-description') + "</span></div>";
            },
            position: {
                target: 'mouse',
                adjust: { x: 5, y: 17 }
            },
            style: {
                tip: { corner: false }
            }
        });
    }

    function updateError() {
        $('.invalid_form').closest('.wrap_input').addClass('error');
        $('#reg_form_pay input').each(function(element) {
            if ($(this).hasClass('invalid_form')) {
                $(this).closest('.wrap_input').addClass('error');
            } else {
                $(this).closest('.wrap_input').removeClass('error');
            }
        });
        updateQTip();
    }

    function validateForm() {
        $("#reg_form_pay").validate({
            rules: {
                Email: { required: true, email: true },
                PhoneNumber: { required: true, checkPhoneNumber: true },
                FirstName: { required: true },
                Surname: { required: true }
            },
            messages: {
                Email: '',
                PhoneNumber: '',
                FirstName: '',
                Surname: ''
            },
            errorClass: "invalid_form",
            errorElement: "div",
            errorPlacement: function (error, element) {
                error.insertAfter(element);
            },
            onkeyup: false,
            showErrors: function (errorMap, errorList) {

                this.defaultShowErrors();
                updateError();
            }
        });
    }

    function privateInit() {
        validateForm();
        console.log('init ok');
    }


    return {
        init: privateInit,
    };
}());

To make this code work I have to call the init method in the view as follows:

<script>
    $(document).ready(function() {
        PersonallInformation.init();
    })        
</script>

Is it possible to avoid having to call init in the view?

UPDATE:
I have rewritten it in the following way:

function library(module) {
    $(function() {
        if (module.init) {
            module.init();
        }
    });
    return module;
}

var PersonallInformation = library(function () {
...
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:52:14+00:00Added an answer on June 12, 2026 at 4:52 am

    The short answer is no

    your validateForm method uses the DOM to do it’s work. If you call the init method prior to document.ready the behavior is unspecified.

    You will have to call some method in document.ready.

    You are not really using the module partern since you are in effect just encapsulating function in another function so you would have the same kind of encapsulation if you moved the entire code between (function (){…}()) to document.ready in which case you could change the last part of the function to

    validateForm();
    console.log('init ok');
    

    Ie inline the init function.

    EDIT

    A rewrite could be something like:

     var setupValidate = (function () { 
        return function(options) {
              var defaultOptions = {
                   qtipOptions : {
                      content: function (api) {
                          var text = $(this).prev();
                          return "<div class='tip_cont'><span class='simple cost'><span class='corner'></span>" + $(text).attr('data-description') + "</span></div>";
                       },
                       position: {
                         target: 'mouse',
                         adjust: { x: 5, y: 17 }
                       },
                       style: {
                          tip: { corner: false }
                       }
                   },
                   formSelector : "#reg_form_pay",
                   invalidFormSelector : 'div.invalid_form',
    
             };
             options = $.extend(defaultOptions,options);
             $.validator.addMethod("checkPhoneNumber", function (value, element) {
    
                if (!value) return true;
                return /^((\+7)|8)(700|701|702|705|707|712|713|717|718,721|725|726|727|777)[0-9]{7}$/.test(value);
             }, "Wrong phone format");
    
             function updateQTip() {
                $(options.invalidFormSelector).qtip();
             }
    
        function updateError() {
            $(options.invalidFormSelector).closest('.wrap_input').addClass('error');
            $(options.formSelector).find("input").each(function(element) {
                if ($(this).hasClass('invalid_form')) {
                    $(this).closest('.wrap_input').addClass('error');
                } else {
                    $(this).closest('.wrap_input').removeClass('error');
                }
            });
            updateQTip();
        }
    
        function validateForm() {
            $(formSelector).validate({...});
        }
    
            validateForm();
            console.log('init ok');
      }
    }());
    

    and you’d then call it like:

    $(function(){
        setupValidate (/*with options if you'd like to change the default*/);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started learning Jquery and am new to writing javascript (I am
I have just started learning Javascript. I want Hello World! to be written to
I have started learning Code Igniter and I am very impressed, and have had
I have just started learning Javascript and I am absolutely overwhelmed with the number
i've started learning about javascript closures, and while experimenting, i realised that the following
i have started to learning Jquery and i can't activate the resizable function. i
I have some php and javascript experience and just started learning curl. Been messing
Started learning javascript yesterday at CodeAcademy and decided I could actually write code. Just
I have just started writing my own JavaScript Framework (just for the learning experience),
I've just started learning javascript and have created a drag and drop using jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.