开发者

Regex to test if an input string contains a certain number of characters

开发者 https://www.devze.com 2022-12-26 04:51 出处:网络
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 whateve

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 v开发者_开发问答alidation:

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


^([^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.)


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消