We help IT Professionals succeed at work.

Regular Expression Validation ASP.Net

I have this expression

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,10})$

Its okay except for the fact that I need it to allow wild card characters.
Comment
Watch Question

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Which ones? I'll assume that you want to allow * and %

The original pattern validates so that the value tested:
1. must not be only numbers, or empty
2. must not be only alphabetic characters
3. must be from 8 to 10 alphanumeric characters

I've changed the 3rd aspect so that it accepts * and % as well as alphanumeric characters.

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9*%]{8,10})$

Author

Commented:
@TerryAtOpus - I need it to accept $ as well. However the one you sent didn't work for "*" or "%" when I tried
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
Do you want it to accept just a single wildcard character? I assumed you wanted the 8-10 character limitation to still apply, but to allow wildcard characters too.

Can you give some examples indicating which values are acceptable and which aren't. Eg ABc%65D* or A*

Author

Commented:
Sorry I forgot I limited it to 8 to 10 characters.

I would need it to accept something like $xxxxx871!

I don't want % or * to be available. My other question is does it validate a specific placeholder or will it check the whole string.
John ClaesSenior .Net Consultant & Technical Analist

Commented:
I use the folowing regex checker

http://regexhero.net/tester/

Checking the Regex from TerryAtOpus I saw indeed that the $ was not included

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9*%$]{8,10})$

Please see the difference.

please play in the checker to see the result
Mohamed AbowardaSenior Software Engineer
CERTIFIED EXPERT

Commented:
Take a look here:
http://stackoverflow.com/questions/2821419/regular-expression-starting-and-ending-with-a-letter-accepting-only-letters

Scroll down, you should see how to create a regular expression that accept certain beginning and end of string.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
I don't want % or * to be available.
If you only want the $ symbol to be allowed in the string, then just add it to the last set of brackets--the way Terry did with * and %. This might be a bit simpler to read/understand:

^(?i)(?=\D*\d)(?=\d*\D)([A-Z\d$]{8,10})$

Open in new window


...where "\d" means "[0-9]" and "\D" means the opposite:  [^0-9]. "(?i)" means turn on case-insensitive matching.

My other question is does it validate a specific placeholder or will it check the whole string.
You question isn't wholly clear, but my initial response is that it will check the whole string because that is the way the pattern is written.

Author

Commented:
Here is an example format of a password. The X's represent some random letter could be capital or lowercase. I am having an issue with it being allowed. It keeps going into the code as invalid format. Below is what I am using now. I apologize if I am making this harder than it needs to be. Also those wild characters and numbers may not necessarily always be in the same place.

 "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9*%$]{8,10})$"

$@X0XXX33!
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
You should simply need to add whichever characters you want to allow to the latter part of my pattern:

^(?i)(?=\D*\d)(?=\d*\D)([A-Z\d$!@]{8,10})$

Open in new window

Author

Commented:
@kaufmed - That worked great however when I changed "@" to "#" it went through when it should have been invalid
Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
I'm still not clear on what your "wildcard" characters are. Did you mean special characters? Can you please explain?

Author

Commented:
Sorry yes I meant special characters like $,@,! Those are the only ones we accept. So it should only allow those 3 characters.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
when I changed "@" to "#" it went through when it should have been invalid
Changed it where:  in the pattern, or in the target string? How have you implemented this pattern in your project?
Web Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011
Commented:
Perhaps this then?

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9$@!]{8,10})$

Author

Commented:
Appreciate you guys help.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.