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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:27:32+00:00 2026-05-11T16:27:32+00:00

When doing validation in JavaScript, how do I keep validation methods that need to

  • 0

When doing validation in JavaScript, how do I keep validation methods that need to handle a bunch of contingencies from becoming unruly?

For example, I have a form with a field that has validation that checks:

  • is the new value a number?
  • Is the value in another field on the
    form > 0 when the current field > 0
  • Is another field on the form == 1 and
    the current field == 0
  • Is another field on the form == true
    and the current field is Mod another
    field == 0

Etc.

Currently, I’ve got a method with a bunch of if/else statements.

I could break each check out into it’s own method and call it from the main validation method, but that would create a ton of new methods.

Update: regardless of whether I use a framework or individual methods, am I still resigned to having the calling validation method filled with If/Else or Switch statements with alert boxes for the failing validations?

  • 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-05-11T16:27:32+00:00Added an answer on May 11, 2026 at 4:27 pm

    Sounds like you might want a rudimentary state machine. One thing you might consider is breaking the validators into the simplest forms:

    function validator(op, options, pass, fail) {
        if (op(options)) {
            pass(options);
        } else {
            fail(options);
        }
    }
    

    Then you can chain them together to form more complex systems.

    function isFooChecked(options) {
        return $(options.foo).is(':checked');
    }
    
    function sayHi(options) {
        alert('hi ' + options.name);
    }
    
    function fail(options) {
        alert(options.name + ' was bad');
    }
    
    function validateName() {
        validator(isFooChecked, { foo: '#someField', name: $('#nameField').val() }, sayHi, fail);
    }
    

    You’ll still have a lot of code, but if you do it right, functions should be small and easy to understand.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A JPanel can be drawn and can receive events on… May 12, 2026 at 2:52 pm
  • Editorial Team
    Editorial Team added an answer You don't need jQuery for that. You don't even need… May 12, 2026 at 2:52 pm
  • Editorial Team
    Editorial Team added an answer The short answer to your question is that it is… May 12, 2026 at 2:52 pm

Related Questions

I have a web form with more than 10 fields that are submitted to
The following .NET 3.5 code, placed in an aspx file, will trigger a JavaScript
We want to share user validation configuration between a Java validation class (for sanity
I have the following models: class Category(models.Model): name = models.CharField(max_length=40) class Item(models.Model): name =

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.