• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

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.
0
aothomas42
Asked:
aothomas42
  • 6
  • 4
  • 3
  • +2
1 Solution
 
Terry WoodsIT GuruCommented:
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})$
0
 
aothomas42Author Commented:
@TerryAtOpus - I need it to accept $ as well. However the one you sent didn't work for "*" or "%" when I tried
0
 
Terry WoodsIT GuruCommented:
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*
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
aothomas42Author 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.
0
 
John ClaesCommented:
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
0
 
Mohamed AbowardaSoftware EngineerCommented:
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.
0
 
käµfm³d 👽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.
0
 
aothomas42Author 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!
0
 
käµfm³d 👽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

0
 
aothomas42Author Commented:
@kaufmed - That worked great however when I changed "@" to "#" it went through when it should have been invalid
0
 
Terry WoodsIT GuruCommented:
I'm still not clear on what your "wildcard" characters are. Did you mean special characters? Can you please explain?
0
 
aothomas42Author Commented:
Sorry yes I meant special characters like $,@,! Those are the only ones we accept. So it should only allow those 3 characters.
0
 
käµfm³d 👽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?
0
 
Terry WoodsIT GuruCommented:
Perhaps this then?

(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9$@!]{8,10})$
0
 
aothomas42Author Commented:
Appreciate you guys help.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 6
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now