Link to home
Create AccountLog in
Avatar of Bobby X
Bobby XFlag for United States of America

asked on

ASP.Net RegularExpression Validators

Hi,

I am looking for a regular expression that validates to ensure that a min. of 2 and max. of 8 alpha characters (including spaces) are allowed without counting any leading and trailing spaces. Following regular expression doesn't seem to work (see ValidationExpression below).  Could someone please tell me what am I missing and provide a correct regex for this?

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtProductName" CssClass="ErrorMessage" Display="Dynamic" ValidationExpression="^[^\s]+[a-zA-Z\s]{2,8}[^\s]+$" ErrorMessage="Minimum 2 and maximum 8 characters allowed." SetFocusOnError="True" />

Many thanks in advance.


Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Try this:
ValidationExpression="^(\s)*[a-zA-Z0-9_\s]{2,8}(\s)*$"

Open in new window

Or
ValidationExpression="^(\s)*[\w\s]{2,8}(\s)*$"

Open in new window



Avatar of Bobby X

ASKER

None of your regex works.
The rule is:  A minimum of 2 and a maximum of 8 Alpha characters (no numbers) and whitespaces in between the characters are allowed.  For example, these are allowed:
"abcdefgh" - this has a maximum of 8 Alpha characters (no numbers)
"ab de gh" - this has a maximum of 8 Alpha characters (no numbers) w/ whitespaces in between
" abcdefgh " - this has a maximum of 8 Alpha characters (no numbers) with 1 or more leading & trailing spaces
" ab de gh " - this has a maximum of 8 Alpha characters (no numbers) w/ whitespaces in between but has 1 or more leading & trailing spaces
"ab" - this has a minimum of 2 Alpha characters (no numbers)
" ab " - this has a minimum of 2 Alpha characters (no numbers) with 1 or more leading & trailing spaces


ASKER CERTIFIED SOLUTION
Avatar of David H.H.Lee
David H.H.Lee
Flag of Malaysia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer