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 8147947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:31:44+00:00 2026-06-06T14:31:44+00:00

After using a few validation libraries I finally decided to roll my own implementation,

  • 0

After using a few validation libraries I finally decided to roll my own implementation, since none of the libraries behaved the way I wanted. The code below works fine, but I think it’s not very idiomatic. The function body is huge and should probably be refactored into smaller chunks.

I am sure there are many ways to improve my code, but I am asking for specific advice on how to break up the large function into smaller functions, so that the code is easier to understand. Obviously I want to add a lot more validation functions, and I don’t want to create a bunch of spaghetti.

The validation should happen on blur of an element. By inserting and removing divs the user is notified of the problem.

The validation function:

(function($) {
  $.fn.extend({
    live_validate: function() {
      return this.each(function() {

        var inputs = $('input', $(this));

        inputs.blur(function() {
          var el = $(this);
          el.siblings('div.feedback').remove();

          if(el.hasAttr('required') && el.val() == '') {
            el.after('<div class="feedback feedback-error">must be present</div>');
            return;
          }    

          if(el.hasAttr('minlength') && el.val().length < el.attr('minlength')) {
            el.after('<div class="feedback feedback-error">is too small</div>');
            return;
          }

          // no validation returned; all good.
          el.filter(":visible").after('<div class="feedback feedback-ok"></div>');

        });

      });
    }
  });
})(jQuery);

This is a quick helper I made to check if an attribute is present. I need to know if the attribute is present at all. Is there a better way to do this?

$.fn.hasAttr = function(name) {
   return (this.attr(name) !== undefined && this.attr(name) !== false);
};

Finally, call the validator on the form I want.

$(document).ready(function() {

    $("form.new_user").live_validate();

}
  • 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-06T14:31:46+00:00Added an answer on June 6, 2026 at 2:31 pm

    I would separate each validator in it’s own function, separating each logic in another function.

    (function($) {
      var validators = {
        required: function(el) {
            // Validation code...
        },
        minlength: function(el) {
            // Validation code...
        } // you can add more validators here..
      };
      $.fn.extend({
        live_validate: function(enabledValidators) {
          return this.each(function() {
    
            var inputs = $('input', $(this));
    
            inputs.blur(function() {
              var el = $(this);
              el.siblings('div.feedback').remove();
              var hasErrors = false;
    
              $(enabledValidators).each(function() {
                 var result = validators[this](el);
                 if(result !== true) {
                   el.after('<div class="feedback feedback-error">' + result + '</div>');
                   hasErrors = true;
                 }
              });
    
              if(!hasErrors) {
                el.filter(":visible").after('<div class="feedback feedback-ok"></div>');
              }
            });
          });
        }
      });
    })(jQuery);
    

    What I did here was to encapsulate each validation in it’s own funcion. It always receives de element and returns true if it’s valid or a string with the error if it’s invalid. I also added an enabledValidators to be able to pass which validators you want to run (you could remove this and execute all the validators always if you want, or only use all the validators if nothing was passed). So you would call it like this:

    $(document).ready(function() {
        $("form.new_user").live_validate(["required", "minlength"]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

after using own custom cms for over 6 years, I decided to go for
I am using eclipse galileo on my macbook pro. After a few minutes it
I'm using jquery validation, however a few particular field values can be copied from
After using F# for a few small problems, I've found it helpful for myself
After using .NET for several years, I've spent the last few months in the
I have recently started to use Eclipse after using IntelliJ for a few years.
I've been using Subversion for a few years and after using SourceSafe , I
After using the ebay API recently, I was expecting it to be as simple
After using PHP for a while now, I've noticed that not all built-in PHP
After using hg qnew and hg qrefresh to create and update a patch that

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.