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

  • Home
  • SEARCH
  • 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 7938339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:47:09+00:00 2026-06-03T22:47:09+00:00

I’ve been writing a javascript function which returns true if the value matches one

  • 0

I’ve been writing a javascript function which returns true if the value matches one of about 4 values (just 3 in the example below). The problem is, when I have just two values the function works correctly, but adding a third breaks the code.

I’m pretty new to javascript and I’m guessing there’s a much better way of doing this? I’ve tried searching but found nothing as of yet.

Any help is much appreciated.

function isValid(elem, helperMsg){

                var sn6 = /[sS][nN]6/;
                var sn5 = /[sS][nN]5/;
                var sn38 = /[sS][nN]38/;

                if(elem.value.match(sn6 || sn5 || sn38)){
                        //do stuff
                        return true;
                }else{

                        return false;
                }
            }

Edit:

Here’s my second attempt with an array:

    function isLocal(elem, helperMsg){
        var validPostcodes=new Array();
            validPostcodes[0]= /[wW][rR]12/;       
            validPostcodes[1]= /[cC][vV]35/;
            validPostcodes[2]= /[sS][nN]99/;
            validPostcodes[3]= /[sS][nN]6/;
            validPostcodes[4]= /[sS][nN]5/;
            validPostcodes[5]= /[sS][nN]38/;
            validPostcodes[6]= /[oO][xX]29/;
            validPostcodes[7]= /[oO][xX]28/;

        var i = 0;

        for (i = 0; i < validPostcodes.length; ++i) {
            if(elem.value.match(validPostcodes[i])){
                    // do stuff
                    return true;
            }else{
                    alert(helperMsg);
                    elem.focus();
                    return false;
            }
        }
    }
  • 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-03T22:47:10+00:00Added an answer on June 3, 2026 at 10:47 pm
    a || b || c
    

    is an expression that evaluates to a boolean. That means that you’re running either match(true) or match(false). You must write it as:

    match(a) || match(b) || match(c)
    

    Another option would be to store them in an array and loop over it. That would mean if the number of patterns grew you wouldn’t have to change code other than the list of patterns. Another approach, though limited to this situation, might be to change the pattern to one that is equivalent to or-ing the three options together (untested, and I’m a bit rusty on regex):

    elem.value.match(/[sSnN][6|5|38]/)
    

    Array based example:

    var patterns = [/../, /.../];
    for (var i = 0; i < patterns.length; ++i) {
        if (elem.value.match(patterns[i])) { return true; }
    }
    

    In real code, I would probably format it like this:

    function isValid(elem, helperMsg){
        var patterns = [/../, /.../],
            i = 0;
        for (i = 0; i < patterns.length; ++i) {
            if (elem.value.match(patterns[i])) {
                return true;
            }
        }
    }
    

    That’s just a habit though since JavaScript hoists variables to the top of their scope. It’s by no means required to declare the variables like that.

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

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.