Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to validate if a string has 8 characters, at least 1 letter, 1 number and 1 special character?

 
Rogerio Kioshi
Ranch Hand
Posts: 690
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to validade if a string has 8 characters, at least 1 letter, 1 number and 1 special character. How to write this rule using Regex?
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
*cough*

But concerning the regex, what have you come up with so far?
 
naved momin
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rogerio Kioshi wrote:I need to validade if a string has 8 characters, at least 1 letter, 1 number and 1 special character. How to write this rule using Regex?

all you need is this regex
but remember from now one , here we are to help you not to provide ready made solutions so from now first show us what you have tried so far then any one would love to solve your problem . k. have a good day bye, put as much special characters you want in the last square bracket.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just in case it didn't come across, password requirements such as this are ridiculous and make passwords less secure. Who makes up this crap?

The only password restrictions that should ever be in place, in my opinion is a minimum length, and preventing the password from being the same as the username. Anything else is just plain stupid.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Attorney General of Texas Child Support website:



Stop the insanity!
 
Koen Aerts
Ranch Hand
Posts: 344
Java Linux Oracle
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And that's why people write down these passwords on a sticky note and hang it on their screen.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a really, really long way to express "we do not want you to log in to our site".
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
naved momin wrote:
Rogerio Kioshi wrote:I need to validade if a string has 8 characters, at least 1 letter, 1 number and 1 special character. How to write this rule using Regex?

all you need is this regex
but remember from now one , here we are to help you not to provide ready made solutions so from now first show us what you have tried so far then any one would love to solve your problem . k. have a good day bye, put as much special characters you want in the last square bracket.



A few issues with the regex....

1. The request was for exactly eight characters -- which is not handled by the regex.
2. I don't think that the requirement "at least 1 letter, 1 number and 1 special character", implies that the special character must come after the number, which must come after the letter -- which the regex requires.
3. I don't think that the plus sign within the character class does what you think it does.

Henry
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you have to use a single regexp? It would be clearer has a bunch of and statements - one for each rule. Some of those rules owuld be a regexp, but a very simple one.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50196
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For something that small, would loops and ifs work quicker than a regex?
 
Rogerio Kioshi
Ranch Hand
Posts: 690
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying. Actually, I gave up using Regex and made a simple method using the following:



I think this way is better for code maintenance.

 
Michael Parmeley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also take advantage of all the special characters being 33-47 decimal (inclusive) in the ascii table.



Results of this code:

numOfSpecial = 5
numOfLetters = 8
numOfUpperLetters = 2
numOfLowerLetters = 6
numOfDigits = 1
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Parmeley wrote:You could also take advantage of all the special characters being 33-47 decimal (inclusive) in the ascii table.

Are you sure ? Without even looking at an ascii table I can tell you it's not true. 33 - 47 covers a range of 15 characters, but Rogerio has 22 characters in his list of special characters.
 
Michael Parmeley
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adrian Burkett wrote:
Michael Parmeley wrote:You could also take advantage of all the special characters being 33-47 decimal (inclusive) in the ascii table.

Are you sure ? Without even looking at an ascii table I can tell you it's not true. 33 - 47 covers a range of 15 characters, but Rogerio has 22 characters in his list of special characters.


Indeed, my mistake. Didn't look at it close enough.
 
Henry Wong
author
Marshal
Pie
Posts: 21498
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rogerio Kioshi wrote:Thanks for replying. Actually, I gave up using Regex and made a simple method using the following:



I think this way is better for code maintenance.


100% agreed. Being able to understand and maintain the code should be of high priority.... However, just in case you are interested in how a regex would look like...



Henry

 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rogerio Kioshi wrote:Thanks for replying. Actually, I gave up using Regex and made a simple method using the following:

I think this way is better for code maintenance.

Seems reasonable to me. Just to point out that you could use regexes in a slightly more meaningful way:
Winston


[Edit]Actually, looking at it:would probably be a lot quicker (with all due deference to Mr. Knuth ).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic