Link to home
Start Free TrialLog in
Avatar of aothomas42
aothomas42Flag for United States of America

asked on

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.
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

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})$
Avatar of aothomas42

ASKER

@TerryAtOpus - I need it to accept $ as well. However the one you sent didn't work for "*" or "%" when I tried
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*
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.
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
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.
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.
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!
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

@kaufmed - That worked great however when I changed "@" to "#" it went through when it should have been invalid
I'm still not clear on what your "wildcard" characters are. Did you mean special characters? Can you please explain?
Sorry yes I meant special characters like $,@,! Those are the only ones we accept. So it should only allow those 3 characters.
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?
ASKER CERTIFIED SOLUTION
Avatar of Terry Woods
Terry Woods
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Appreciate you guys help.