views:

472

answers:

1

Hi,

I have below code in vb.net for validating alphanumeric characters, It is working fine for all alphanumeric characters, However I want to allow user, so that he can insert "*". Please provide me the new regular expression for the above issue.

 <tr>
                <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="revSearchCourse" runat="server" Display="Dynamic"
                        ControlToValidate="txtSearchCourse" ValidationExpression="^[0-9a-zA-Z]+$" ErrorMessage="The Course Name Should be Only Alphanumeric characters"></asp:RegularExpressionValidator>
                </td>
 </tr>
+2  A: 

Just add the * character inside the character class, like this: ^[0-9a-zA-Z*]+$

Ahmad Mageed
Since '*' is a zero-or-more of the previous character, won't that mess things up?
Yannick M.
In this case the * doesn't need to be escaped since it is inside a character class (or group). Outside of a character class it holds a special meaning as a meta-character, in which case it would need to be escaped.
Ahmad Mageed
Thanks Dear! for your help.
MKS
Good to know ------ 15 chars
Yannick M.
OTOH, if you had a dash inside a group, that holds a special meaning, namely it acts as a range. Escaping that would be ideal, or placing it at the start or end, such as [ACF-] which matches A,C,F and the dash, whereas [A-CF] would match A,B,C,F and no dash. In this case it would be written as [A\-CF]
Ahmad Mageed