Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
Check is string contains special characters and at least 2 characters amongs digit and letter

Hi guys I trying to check if password contains special caracters and at least 2 characters (digit or letter) So like this:

("&#€$&&÷") ====> false

("&#&'&*#5") ====> false

(">~<<`<•5t") ====> true

("{\><>\tt") =====> true

("65%#^$*@") ====> true

("7373673") ====> false

("7267373~") ====> true

I've tried this regular expression but It seems not working:

/^((?=.*\d{2})|(?=.*?[a-zA-Z]{2}))/
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can assert the 2 occurrences of a character or digit in the same lookahead, and then match at least a single "special" character.

Using a case insensitive pattern:

^(?=(?:[^a-z\d\n]*[a-z\d]){2})[a-z\d]*[~!@#$%^&*()_+<>•`{}\\][~!@#$%^&*()_+<>•`{}\\a-z\d]*$

The pattern matches:

  • ^ Start of string
  • (?:[^a-z\d\n]*[a-z\d]){2} Assert 2 occurrences of either a char a-z or a digit. The [^a-z\d\n]* part negates the character class using [^ to prevent unnecessary backtracking
  • [a-z\d]* Match optional chars a-z or a digit
  • [~!@#$%^&*()_+<>•`{}\] Match a special character
  • [~!@#$%^&()_+<>•`{}\a-z\d] Match optional allowed chars
  • $ End of string

Regex demo

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!