-3

I'm working on an app that needs a password validation.

I should check if the password string contains only alphanumerical and puntionations, and at least it should be a combination of letters and numbers to pass.

Can someone help me and share a regex that will work for this validation?

Thanks

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
  • 2
    If you search on Google you will get a similar answer in 2 minutes. This is something very general. – Theo Feb 26 '16 at 23:41

1 Answers1

2

An idea with use of lookaheads.

^(?=\D*\d)(?=.*?[a-zA-Z])\p{Graph}+$

Or as a Java string:

"^(?=\\D*\\d)(?=.*?[a-zA-Z])\\p{Graph}+$"
  • (?=\D*\d) the first lookahead checks for a digit
  • (?=.*?[a-zA-Z]) the second for at least one alpha

From ^ start to $ end \p{Graph} + one or more.

  • \p{Graph} consists of [\p{Alnum}\p{Punct}]
  • \p{Alnum} alphanumeric characters [A-Za-z0-9]
  • \p{Punct} one of !"#$%&'()*+,-./:;<=>?@[]^_`{|}~

See demo at regexplanet (Java)

bobble bubble
  • 16,888
  • 3
  • 27
  • 46