views:

62

answers:

2

So, I basically would like to test to see if a string contains a range of alphanumeric characters. It's to be used as a client-side validation and I don't want to prevent users from entering whatever they want. Best to give examples of what should/should not pass validation:

So to be specific, the expression I'm looking for is to test to make sure string contains anywhere from 3 to 10 alphanumeric characters. I'd like to plug into an ASP.NET client side validator.

NOTE: quotes not part of input (but could be!)

  • " f o o " should pass since there are 3 chars
  • "f_0_0" should pass
  • " fo " should not
  • "F......o......o......b.....a......r" should pass

thx

+1  A: 
^([^a-zA-Z0-9]*[a-zA-Z0-9][^a-zA-Z0-9]*){3,10}$

Allows for exactly 3-10 alphanumeric characters, each surrounded by an arbitrary number of non-alphanumeric characters.

(Untested, but it should conform to the JScript subset of the standard .net Regex syntax, as required by the RegularExpressionValidator. Unfortunately, the shorthands \w and \W cannot be used since they include the underscore as an alphanumeric character.)

Heinzi
does the trick thanks!
Dan
A: 

I'm not familiar with ASP.NET client-side validators, so I'm not sure if you need to do this in a regex, but potentially an easy solution is as follows:

  1. Remove all non-alphanumeric characters (regex replace [^0-9A-Za-z] with nothing).
  2. Check if string length is 3 or greater.
Chad Birch