Click here to Skip to main content
15,041,548 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the following code, the regular expression throws error on selection of a value from DDL.
There is some wrong in validation expression. How to use the correct validation expression for the data in this DDL. In DDL there is small characters, an -, brackets and space.

<asp:DropDownList ID="DropDownList7" runat="server"
        style="z-index: 1;top: 246px; left: 225px; position: absolute; height: 18px; width:148px"
         <asp:ListItem>(7-14 days)</asp:ListItem>
         <asp:ListItem>(4-8 days)</asp:ListItem>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator5" runat="server"  Display='None'
            ControlToValidate="DropDownList7" ErrorMessage="Select Delivery Days"
            ValidationExpression="[0-9 a-z \s \d-_ ][(][)]{1,50}" Font-Size="X-Small"></asp:RegularExpressionValidator>

The regex for space is \s.
For e.g. the regex for matching alphanumeric and spaces would be Regex.Replace(q, @"[^a-zA-Z0-9\s]", string.Empty);
Maciej Los 19-Mar-14 3:06am
Abhinav S 19-Mar-14 3:47am
Thank you.
S.Rajendran from Coimbatore 19-Mar-14 3:18am
I used like this : ValidationExpression= "[^a-zA-Z0-9\s][d\-_][(][)]" not working

^                      # Start of string
\s*                    # Optional leading whitespace, don't capture that.
(?=                    # Assert that...
 [a-zA-Z0-9'/\\&.-]    # the next character is allowed and non-space
(                      # Match and capture...
 [a-zA-Z0-9'/\\&.\s-]{3,} # 
(?<=\S)                # Assert that the previous character is not a space
\s*                    # Optional trailing whitespace, don't capture that.
$                      # End of string
Your problem is a different one: the brackets. () are used for catching some data, but here you want to match a bracket. You must use \( and \). [] stand for character ranges, if you want to match them, you must escape them also \[\]. I do not know if literal spaces are acceptable in a character range, better omit them.
I'd write
S.Rajendran from Coimbatore 19-Mar-14 6:42am
This worked for me: "[a-z 0-9 \s \d-_ ()]{1,50}" Thank you all.
Bernhard Hiller 19-Mar-14 6:47am
OK, then click "accept answer".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900