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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:01:21+00:00 2026-05-31T20:01:21+00:00

I know this has been asked before but all the answers I found didn’t

  • 0

I know this has been asked before but all the answers I found didn’t work correctly on some cases.

I need to validate a password field and allow only certain characters.

The validation is done in both client and server side. On PHP I also couldn’t get the regex to perform as I needed, so what I did was split the password in an array of characters and check each one against an array of allowed chars. If any char is not on the array, it returns false.

The allowed characters are 0-9, a-z, A-Z and ` ! ” ? $ % ^ & * ( ) _ – + = { [ } ] : ; @ ~ # | < , > . ‘ / \ (whitespaces are not allowed).

Now I need something similar in Javascript and can’t figure how to do it.

Regex isn’t working correctly since it sometimes fails when there aren’t invalid characters (probably because of the chars order?).
The current regex I got from this site is

/^[A-Za-z\s`~!@#$%^&*()+={}|;:'",.<>\/?\\-]+$/

So, valid passwords could be, for example

urjv()$%...84
40#"!!mbn'"{}
890$%/\|`doc
,-=?wht(!{})=

Is there any other solution for this on JS, or is the regex wrong in fact?

Thanks for your help!

  • 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-31T20:01:22+00:00Added an answer on May 31, 2026 at 8:01 pm

    The following characters need to be escaped in a character class:

     - / [ \ ] ^
    

    Note that the characters that need to be escaped are different when you’re creating a Regex object using new.

    You are not escaping these characters when they are present, and some of them are not even included. Also, you’re including whitespace with the \s when you specified that it isn’t allowed. Finally, unless I looked at your regex too quickly, you did not include numbers. To address your concern that the order is what causes your regex to fail, order is not relevant in a character class.

    A corrected regex:

    /^[0-9a-zA-Z`!"?$%\^&*()_\-+={\[}\]:;@~#|<,>.'\/\\]+$/
    

    Here is a “test-suite”. I don’t claim it to be fool-proof but it seems to work fine:

    http://jsfiddle.net/radu/AvGtY/

    Change up the dictionary as needed and you should be good to go to test massive amounts of regexes. Note that as currently setup it only really checks consistency – that is, it only checks if the regex finds everything that is in the dictionary string.

    Also, you need to check passwords server-side as well. Your client side code can be bypassed easily so you should always do validation server-side to prevent that. Client-side code is useful for immediate feedback but not much else in this case.

    As pointed out by Qtax, the following would be a tad shorter and accomplish the same thing:

    /^[-0-9a-zA-Z`!"?$%&*()_+={[}\]:;@~#|<,>.'\/\\^]+$/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before but the other solutions didn't work
I know this has been asked much the same before but no answers seem
I know this question has been asked before and I read all the answers
I know this has been asked before, but I've followed all guides and somehow
I know this question has been asked multiple times but all the answers seem
Hey all, I know this question has been asked a million times before but
I know this question has been asked before, but I'm having some difficulty getting
Ok, I know this has been asked before but after searching I couldn't find
I know this probably has been asked before but I am having issues with
I know this has been asked before, but I just cant seem to find

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.