0

I have a regex that is working fine:

^(?!\\d+$)\\S{8,}$
  • (?!d+$) // Deny numbers only
  • \S{8,} // Allow special characters but white spaces and a minimum of 8 characters;

But I need to add one more thing: Deny if the string is special characters only.

tried to apply the same logic as the "only numbers deny", an add

(?!\\S+$))

to the existing regex, but didn't work as expected.

obs: I will work with different languages, so the "special characters" would be:

! @ # $ % & * ( ) _ - = < > , . ? } ] { [ +

PlayHardGoPro
  • 2,504
  • 7
  • 38
  • 78

1 Answers1

0

Deny if the string is special characters only.

For this add another negative lookahead assertion:

^(?!\\d+$)(?![-!@#$%&*()_=<>,.;]+$)\\S{8,}$

(?![-!@#$%&*()_=<>,.;]+$) is negative lookahead to fail the match if input contains only those listed characters.

anubhava
  • 713,503
  • 59
  • 514
  • 593