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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:35:21+00:00 2026-05-11T10:35:21+00:00

So I want to build a form validation class/object in javascript. The way I

  • 0

So I want to build a form validation class/object in javascript. The way I see it working would be something like this:

var form=new Validation();  form.addField('name','Your name','required'); form.addField('email','Email Address','is_email'); ......... form.validate();  

I was thinking that the validation class would be defined something like this?

validation {   var fields=new Array();   var labels=new Array();   var rules=new Array();   var count=0;    function addField(field,label,rule)   {     this.fields[count]=field;     this.labels[count]=label;     this.rules[count]=rule;     this.count=count+1;   }    function validate()   {     var valid;     for (x=0; x< count; x++)     {       valid=false;       switch (this.rules[x])       {          case 'required':            valid=this.validate_required(this.fields[x]);            break;           case 'email':            valid=this.validate_email(this.fields[x]);            break;        }         if (! valid)          this.addError(this.fields[x],this.rules[x],this.labels[x]);      }       if (this.hasErrors())        return false;        else        return true;    }  .......... } 

I’m aware that this probably isn’t possible as it is right now. My question is, what can I do to so the first block of code (which creates a new instance of Validation and adds the rules to it) would work?

Thanks in advance.

  • 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. 2026-05-11T10:35:21+00:00Added an answer on May 11, 2026 at 10:35 am

    You’re just missing some OO notation. Lines marked with a '// ***' comment are the ones I changed from your original code sample:

    function Validation() // *** {   var fields=new Array();   var labels=new Array();   var rules=new Array();   var count=0;    this.addField = function (field,label,rule) // ***   {     fields[count]=field; // ***     labels[count]=label; // ***     rules[count]=rule;   // ***     count=count+1;   }    this.validate = function () // ***   {     var valid;     for (x=0; x< count; x++)     {       valid=false;       switch (rules[x]) // ***       {          case 'required':            valid=this.validate_required(fields[x]); // ***            break;           case 'email':            valid=this.validate_email(fields[x]); // ***            break;        }         if (! valid)          this.addError(fields[x],rules[x],labels[x]); // ***      }       if (this.hasErrors())        return false;        else        return true;    } } 

    Oh and I didn’t see validate_required(), validate_email(), addError() nor hasErrors(). Might wanna add those.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to build something like a network scanner, so that I see in
I want to build my WP theme with a built in contact form. Something
For my framework I've written down this concept to solve the form validation problem.
I want to build a small ORM ( Object Relational Mapping ) framework in
I want to use a temp directory that will be unique to this build.
I have to build a form where I want a drop down menu list,
I want to build a lightweight linux configuration to use for development. The first
I want to build a bot that asks someone a few simple questions and
I want to build my own custom log4j (network) adapter to solve my problem
I want to build an executable to distribute to people without python installed on

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.