0

i have a regexp for password validation ( Regexp Java for password validation with extra special characters)

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

The issue with this is, if i say

 "Xyz.123".matches(pattern);

This returns false

However, if i say

  "Xyz.123$".matches(pattern);

This returns true

'.' is not a valid special characters in my case. But if my password has a valid special character along with '.' it returns true

Community
  • 1
  • 1
Shoaib
  • 3
  • 2

1 Answers1

0

You have \\S+, which means "any non-space character." . satisfies that. You also need the $ to satisfy the third pseudo-condition (lookahead). . is not in that, but $ is.

EDIT: If you do not want any periods at all, change the last pseudo-condition to (?=[^\\s.]+$)

Explosion Pills
  • 176,581
  • 46
  • 285
  • 363
  • I am very new to REGEX. Can you please provide me the correction in the regex or actual regex. Thanks – Shoaib May 10 '13 at 14:15
  • @Shoaib I don't understand what the problem is. You don't want to allow periods? – Explosion Pills May 10 '13 at 14:16
  • 1. Yes, I don't want to allow periods. – Shoaib May 10 '13 at 14:24
  • @Shoaib change `(?=\\S+$)` at the end to `(?=[^\s.]+$)` – Explosion Pills May 10 '13 at 14:29
  • 1. Yes, I don't want to allow periods. 2. If a user enters period in his password, the regex does not match his password and returns "FALSE" , this helps me notifying the user to re-enter password. 3. But, the issue is, if a user enters a password with a period, along with some other special characters(that are permitted in my regex ), say the user enters password "Password@123.321" Then the regex does match this new password and returns TRUE. which is not an expected behaviour since i do not have a period in my REGEX. Thanks – Shoaib May 10 '13 at 14:35
  • @Shoaib why did you ignore my last comment? Change `(?=\\S+$)` to `(?=[^\\s.]+$)` – Explosion Pills May 10 '13 at 14:44
  • Thanks a lot :-) It works. I missed your comments, was editing my reply and the comments did not show up. – Shoaib May 10 '13 at 18:16
  • Hi, This solution is working for period. I am facing same issue with other special characters which are not allowed in my regex(e.g. ¡¢£¥¦§¨ª«¬). Can you please provide a universal solution for the not allowing special characters apart from the one listed in the Regex. Thanks – Shoaib May 15 '13 at 12:18
  • @Shoaib you would have to create a whitelist of allowed characters instead of a blacklist (i.e. `(?=[0-9A-Za-z@#$%^&+=!\\*_?|~(){}/<>:\"\',\\[\\]\`;\\\\\\\\-]+$)` at the end) – Explosion Pills May 15 '13 at 13:23