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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:51+00:00 2026-05-26T21:28:51+00:00

I have 5 functions that check user input against regular expressions…these regular expressions have

  • 0

I have 5 functions that check user input against regular expressions…these regular expressions have been abstracted out and put here as suggested in previous post.

Now I want to abstract out the functionality and put the code into an associative array per the previous post as well..however their seemed to be some contention as to whether this is best practice? Is this O.K to do?

Is it good practice to put these functions into an associative array of functions? The previous post doing this is not up..as I’m guessing they decided against it.

But basically how to do you group similar methods together properly.

EDIT 1: // consolidated regular expressions

var patterns = {   
  name: /^[a-zA-Z-\s]{1,20}$/, 
  email: /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{1,4}$/,
  pass: /.{6,40}/,
  url:  /^[-\w&:\/\.=\?,#+]{1,}$/,
  aml:  /<(.+)_([a-z]){1}>$/
}; 

// checks full name which allows characters and dashes

function check_name(text,id,res) 
  {
  for(var d=0;d<=0;d++)
    {
    if(!patterns["name"].exec(text.value))
      {
      o2(id,res);
      return 0;
      }
    }
    return 1;
  }

// checks for valid email form

function check_email(text,id,res)
  {
  if(!patterns["email"].exec(text.value))
    {
    o2(id,res);
    return 0;
    }
  return 1;
  }

// checks for password length

function check_pass(text,id,res)
  {
  if(!patterns["pass"];.exec(text.value))
    {
    o2(id,res);
    return 0;
    }
  return 1;
  }

// checks for valid url form

function check_url(text,id,res)
  {
  if(!patterns["url"].exec(text.value))
    {
    o2(id,res);
    return 0;
    }
  return 1;
  }

// checks for aml form

function check_aml(text)
  {  
  if(a=patterns["aml"].exec(text))
    {
    if(a[2]=='p')
      {
      return 0;
      }
    else if (a[2]=='f')
      {
      return 1;
      }
    }
  else
    {
    return 2;
    }
  }
  • 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-26T21:28:52+00:00Added an answer on May 26, 2026 at 9:28 pm

    I don’t think there are any issues with creating a common place to store your patterns. This in fact seems like a good approach. However I don’t see the need to have your individual checking functions be made aware of the global storage. They should instead be made simply aware of the pattern they are checking and the source against which they check. If you take this approach your checking code can be dramatically simplified

    var make_check = function (pattern) {
      return function (text, id, res) {
        if(!pattern.exec(text.value)) {
          o2(id,res);
          return 0;
        }
    
        return 1;
      };
    };
    
    var check_name = make_check(patterns.name);
    var check_email = make_check(patterns.email);
    var check_pass = make_check(patterns.pass);
    var check_url = make_check(patterns.url);
    

    Here I’ve removed the notion of the global storage from the checking function. Since the rest of the code is so similar it can be abstracted out to a single method which is passed the pattern it’s looking to match.

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

Sidebar

Related Questions

I have a log function on my admin panel that checks user input for
When a user is inputting something, I need to check user input against the
I have a function that takes in user input via std::cin: std::getline(std::cin, in); and
I have written a set of javascript functions that allow me to validate user
I need to validate some user input that is encoded in UTF-8. Many have
I have a function that checks user input and wanted to know if it
I have a javascript function (function1) that checks some global variables (can the user
I have this simple code that speaks for itself. <script language='javascript"> function check() {}
I have functions that contribute to small parts of a figure generation. I'm trying
I have several functions that I wrote and I use regularly on my servers,

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.