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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:47+00:00 2026-05-27T23:00:47+00:00

I am trying to find a method to validate a string, using a list

  • 0

I am trying to find a method to validate a string, using a list of allowed characters only.

var valid = validate('This is my string!', 'abcdefghijklmnopqrstuvwxyz');

function validate(value, allowed_chars){
            var reg = new RegExp('[' + allowed_chars + ']');
            var valid = reg.test(value);
            return valid;
}

In this case, I wish for the function to return false because it has an exclamation mark in the string and that hasn’t been put in the allowed characters. Any ideas?

Help is much appreciated, Thankyou!

I do not wish to use a loop to check each character.

  • 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-27T23:00:48+00:00Added an answer on May 27, 2026 at 11:00 pm

    Your regex matches only single character. Add the quantifier.

    var reg = new RegExp('[' + allowed_chars + ']+');
    

    ‘+’ means ‘one or more occurences of this’

    You also want to test if this pattern matches the entire string. Add markers for beginning and end of line.

    var reg = new RegExp('^[' + allowed_chars + ']+$');
    

    You might also want to add upper case letters and spaces to your regex. Use an online regex tester to debug your regular expressions.

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

Sidebar

Related Questions

I am trying to get this method in a String Filter working: public function
I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element,
I am trying to use the List.Find method to find a match with an
I am trying to validate a directory with C++. http://php.net/manual/en/function.is-readable.php bool is_readable ( string
i've been trying to find a method in C# to measure the size of
Trying to find a good name for a method swapping each coordinate X and
I'm trying to implement method Find that searches the database. I forgot to mention
I'm trying to find a way to fake the result of a method called
I'm trying to write (or just find an existing) PHP method that can take
I'm trying to find an item in a list of values based on another

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.