Regular Expression For Min 8 and Max 16 Any Characters

rito1
rito1 used Ask the Experts™
on
Hi All,

I am working on a C# ASP.Net form and want to implement a validation control to achieve the following validation:

Min character length: 8
Max character length: 16

There are restraints on what character could be used as the data will be stored using an NVARCHAR type within my table.

Please could anyone suggest the Regexp to achieve this?

Kind Regards,

Rit
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
rito1,

You can determine whatever pattern you want to use for characters, but say you had something like this [a-fA-F3-9].  You can make this constrained size wise using {min, max}.  See code snippet for full code.

Regards,
Kevin
"^[a-fA-F3-9]{8, 16}$"

Open in new window

Author

Commented:
Hi Kevin,

Thank for your time.

My experiences with Regexp is very minimal. I have tried to implement your suggestion and cant get it to work as expected.

I undertands the {min,max} part but not the actual declaration of characters [a-fA-F3-9]. If I wanted the user to be able to input any characters within the range of a-z, A-Z or 1-9 then what would the declaration be?

Many thanks,

Rit
Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
You can use the same format which says it can be any of the characters in between [ ].  The a-f just allows shorthand to say all characters between a and f without writing all of them out; therefore, the updated syntax is pretty much how you have written in your post just between brackets. :)
"^[a-zA-Z1-9]{8, 16}$"

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Author

Commented:
Thanks Kevin, though it still doesn't seem to work for me.. This is my validation control...

When I add say abcdef123 I would expect this to validate for me but it doesn't.

Can you see anything that I could be doing wrong?

Thanks,

Rit
<asp:RegularExpressionValidator 
ID="RegularExpressionValidator1" 
runat="server" 
ErrorMessage="The field 'Username' requires the correct format." ControlToValidate="txtUsername" 
ValidationExpression="^[a-zA-Z1-9]{8, 16}$" 
Display="None">
</asp:RegularExpressionValidator>

Open in new window

Chief Technology Officer
Most Valuable Expert 2011
Commented:
Just tested and looks like I had to include the 1-9 in front of the a-z.
"^[1-9a-zA-Z]{8,16}$"

Open in new window

Author

Commented:
Thanks, Rit

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial