This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to validate if a string has 8 characters, at least 1 letter, 1 number and 1 special character? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to validate if a string has 8 characters, at least 1 letter, 1 number and 1 special character?" Watch "How to validate if a string has 8 characters, at least 1 letter, 1 number and 1 special character?" New topic
Author

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

Rogerio Kioshi
Ranch Hand

Joined: Apr 12, 2005
Posts: 689
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?


SCEA 5 (part 1), SCBCD, SCWCD, SCJP, CLP, CLS
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1818
    
    7

*cough*

But concerning the regex, what have you come up with so far?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

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.


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61594
    
  67

From the Attorney General of Texas Child Support website:



Stop the insanity!
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

And that's why people write down these passwords on a sticky note and hang it on their screen.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3611
    
  60

That's a really, really long way to express "we do not want you to log in to our site".
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30894
    
158

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39773
    
  28
For something that small, would loops and ifs work quicker than a regex?
Rogerio Kioshi
Ranch Hand

Joined: Apr 12, 2005
Posts: 689
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

Joined: Aug 20, 2004
Posts: 14
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

Joined: May 30, 2012
Posts: 679
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

Joined: Aug 20, 2004
Posts: 14
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
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

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

Joined: Mar 17, 2011
Posts: 8176
    
  23

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 ).


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to validate if a string has 8 characters, at least 1 letter, 1 number and 1 special character?