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

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What you are describing is a situation where you have… May 12, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer Actually CRT was not the issue here. What caused the… May 12, 2026 at 10:52 am
  • Editorial Team
    Editorial Team added an answer Your problem is that '-[NSWindowController showWindow:]' does not block waiting… May 12, 2026 at 10:52 am

Related Questions

I'm trying to build a very, very simple micro-webapp which I suspect will be
I'm trying to work out if there's a built in way in ASP.NET MVC
I don't know very much about c#. I am updating a page built by
Looking for a bit of advice on where to take a current webapp which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.