• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

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

 
Ranch Hand
Posts: 701
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*cough*

But concerning the regex, what have you come up with so far?
 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67732
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67732
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Attorney General of Texas Child Support website:



Stop the insanity!
 
Ranch Hand
Posts: 344
Oracle Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And that's why people write down these passwords on a sticky note and hang it on their screen.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author & internet detective
Posts: 41763
885
Eclipse IDE VI Editor Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 78408
374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For something that small, would loops and ifs work quicker than a regex?
 
Rogerio Kioshi
Ranch Hand
Posts: 701
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic