• 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
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Java validation not to have alpha numeric sequence

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,


looking for java validation (not javascript) that checks in the password not to have alphabet or numeric sequence .
for eg., 12345, abcde, abcdefg, 123456 should not be allowed
1234, abcd, 2345, cdef are allowed.

I mean sequence of alphanumeric characters of length upto 4 are allowed and sequence of
alphanumeric characters of length greater than 4 should not be allowed in password .

I have tried in Regex, but couldn't achieved .

could any one suggest me.

Thanks in advance
Kolapalli
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show us what you have tried.
 
Marshal
Posts: 79826
388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Agree with Bear: please show us what you have already. Also please explain what you mean by alphanumeric sequence. Why are you allowed cdef and not abcde? Do you mean a sequence of 5+ characters? Are you allowed bcdef? What about edcba?
 
Ranch Hand
Posts: 104
2
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems you can't check for consecutive characters using regex. But you can write your own simple algorithm for this. To check for consecutive character you can use ASCII value of an character, since [0-9] and [Aa-Zz] are having consecutive ASCII values and are constants.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omkar Shetkar wrote:It seems you can't check for consecutive characters using regex..



Correct. But it is trivial to loop through the String and detect a run.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

kumar kolapalli wrote: looking for java validation (not javascript) that checks in the password not to have alphabet or numeric sequence.
for eg., 12345, abcde, abcdefg, 123456 should not be allowed
1234, abcd, 2345, cdef are allowed.

I mean sequence of alphanumeric characters of length upto 4 are allowed and sequence of
alphanumeric characters of length greater than 4 should not be allowed in password .

I have tried in Regex, but couldn't achieved .


Probably because you're trying to do everything at once.

Assuming you're only talking about sequences, and a password of "1234abcd" is perfectly valid, then my suggestion would be to set up regexes for 5 or more alphabetic or numeric characters - for alphabetic it would be something like "[a-zA-Z]{5,}" - and reject the password if you find one.

I suggest you have a look at the Pattern (java.util.regex.Pattern) and Matcher (java.util.regex.Matcher) classes.

Alternatively, as Omkar says, it would be quite simple to write an algorithm for yourself.

Winston
 
Richard Tookey
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:
Assuming you're only talking about sequences, and a password of "1234abcd" is perfectly valid, then my suggestion would be to set up regexes for 5 or more alphabetic or numeric characters - for alphabetic it would be something like "[a-zA-Z]{5,}" - and reject the password if you find one.



Sorry Winston but I don't think this in any way applies to detecting a sequence; it would just detect any 5 alpha numerics regardless of whether or not they are a sequence. The best I can do is to use Matcher.find() with a regex of "abcde|bcdef|defgh| ...... |01234|12345|23456|..... " (the ... just meant to save me writing out all the sequences) . Even though this works to my mind it is an abuse of regular expressions.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Richard Tookey wrote:Sorry Winston but I don't think this in any way applies to detecting a sequence;


Ah, you're quite right; I misunderstood the problem.

@kumar: This definitely doesn't sound like a regex problem. If this only needs to deal with ascii characters, then you can probably use the fact that consecutive alpha and numeric characters have consecutive numeric values, and if "AbCdE" is also invalid, just convert to lower case (or upper case) before you do the check.

Winston
 
kumar kolapalli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,Thank you all for responding to this post.
Majorly my requirement was to generate password length in range in ( 6,16) among the allowed characters,
String chars = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789$#%&@.";
and validating the generated random password with the rules, like
1.May not contain English dictionary words longer than 4 letters: matched case insensitive, matched forwards and backwards.
2. Must have at least 2 of the following characteristics:1 digit, 1 non alphanumeric character, 1 upper case letter, 2 lower case letters
3. May not contain your username: matched case insensitive, matched forwards and backwards.
4. May not contain common keyboard sequences (eg. 12345, aaaaa): matched case insensitive, matched forwards and backwards

Was able to achieve the points
1, using normal length validation
2,using regex,
3,string equality (also in reverse)
But unable to write regex for 4th point

Anyway I will try,
1. to write loop and validate against ASCII char and try to resolve the issue
2. try the way suggested by Winston by using groups
Thank you
Kumar
 
Winston Gutkowski
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

kumar kolapalli wrote:4. May not contain common keyboard sequences (eg. 12345, aaaaa): matched case insensitive, matched forwards and backwards


Then before you write one line of Java code, you'd better be absolutely clear what those sequences are, because "common keyboard sequences" is NOT sufficient.

As you can see, Richard and I have already put different interpretations on what your original post meant, so it's really important that, at the very least, YOU know. Furthermore, you should probably document it for the next person who has to read your program.

HIH

Winston
 
See where your hand is? Not there. It's next to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic