Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular Expression non-alphanumeric chars

Status
Not open for further replies.

mrp9090

Programmer
Joined
May 22, 2006
Messages
71
Location
GB
I have a regular expression that checks that a password is 8-15 chars and contains at least 1 upper case char, 1 lower case char and 1 number. I need to add to this the condition that there must be at least 1 non-alphanumeric character (*&^%$ etc). How do I do this? Here is my current regular expression :

(^.{0,7}$)|(^.{15}.+$)|([^a-zA-Z0-9])|(^[^A-Z]+$)|(^[^a-z]+$)|(^[^0-9]+$)
 
It is not impossible (constructed programmatically) to make one grand regexp for it, however, it would be very long and of no merit in terms of clarity and maintenance. It would be much better to do multi-step, less-ambitious, yes, but with much clarity and ease to maintain. Like this.
[tt]
dim s, b, rx
s="xxayAz1p%qqq" 'get from something else
b=true
set rx=new regexp
with rx
.ignorecase=false
.pattern="[a-z]" : b=b and .test(s)
.pattern="[A-Z]" : b=b and .test(s)
.pattern="[0-9]" : b=b and .test(s)
.pattern="[^A-Za-z0-9]" : b=b and .test(s)
b=b and (len(s)>=8) and (len(s)<=15)
end with
'b true valid, b false invalid
[/tt]
 
You expression is basically looking for error states:
(^.{0,7}$) shorter than 7 characters?
|
(^.{15}.+$) Longer than 15 characters?
|
([^a-zA-Z0-9]) Anything in it not a-zA-Z0-9?
|
(^[^A-Z]+$) Does it not have a capital letter?
|
(^[^a-z]+$) Does it not have a lower case letter?
|
(^[^0-9]+$) Does it not have a number?

If you can create a set of non-alphanumeric characters you want to add ,then there are two places you need to add them, the third group that checks for characters not allowd in the string and a new group at the end that checks that one of the characters is actually in the string, like so:
(^.{0,7}$) shorter than 7 characters?
|
(^.{15}.+$) Longer than 15 characters?
|
([^a-zA-Z0-9[highlight]\*\&\^\%\$[/highlight]]) Anything in it not a-zA-Z0-9 + special characters?
|
(^[^A-Z]+$) Does it not have a capital letter?
|
(^[^a-z]+$) Does it not have a lower case letter?
|
(^[^0-9]+$) Does it not have a number?
[highlight]|
(^[^\*\&\^\%\$]+$) Does it not have any of the special characters?[/highlight]

I went ahead and escaped them all because I couldnt rmemeber on a few of them. Hope that helps,

-T

barcode_1.gif
 
Can I test for all special characters, or will testing for some of them mess up the regular expression (ones like ^$(){}[]+ etc)?
 
I am not advocating my scheme, but your pattern is ill-advised to start with. You use "|" kind of "or". It will validate any component of the pattern. It is not what you want. But every component of it must validate the _whole_ set of criteria, _not_ just one of the criterion. Or I am reading it too casually.
 
How do I add to my original regular expression the condition that spaces cannot be in the string? I tried adding ([^ ])| to my regular expression without any success.
 
I cannot answer it because I don't think your original pattern would validate anything like you seem to validate. How about telling us what it would not validate? Would a string like "a" be invalidated? (length only 1, only a lower case character without a number or uppercase.) If you cannot determine what its validation base is, any effort to extend it would be furtile.
 
Tsuji,

The original pattern seems to work, as far as it rejects any string that is not 8-15 chars, and/or has < 1 uppercase char, and/or has < 1 lowercase char, and/or has < 1 numeric digit.
 
Okay, I see what you're getting at. You use negation of you validation criteria. So if the pattern let pass (validate), it means rejection. In that case, you add simply this.
[tt] ([[highlight] [/highlight]])|...[/tt]
 
please keep related questions in one post
thread333-1233346


General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
mrp9090 said:
Can I test for all special characters, or will testing for some of them mess up the regular expression (ones like ^$(){}[]+ etc)?

As long as you escape thecharacter, they will not be treated as special characters. That is whyI had escaped them in my highlighted example above.

barcode_1.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top