Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
Hello,

We want the regular expression like:

First character can be only a-zA-Z
Not to allow special characters other than "space" and "hyphen(-)"

We wrote the regexp like "^[a-zA-Z][\w\s-]+"

VB
<asp:RegularExpressionValidator ID="rev" runat="server"
                                            ControlToValidate="txtDesignation" Display="Dynamic" Text="!" ForeColor="Red"
                                            ErrorMessage="Might be Alphanumeric & First character must be a letter!" BackColor="Yellow"
                                            ValidationExpression="^[a-zA-Z]\S-$" ValidationGroup="insert"/>



It was working properly according to my requirement, but, when we enter only single character it is not accepting to enter.

Can you please give the suggestions?
Posted

^[a-zA-Z][a-zA-Z\s\-]*$
 
Share this answer
 
v2
Comments
Brian A Stephens 16-Jun-14 15:28pm    
This also corrects the issue of allowing underscores, which presumably he did not want. Now, if he truly wants to allow "spaces" rather than any whitespace, that's another change.
Just change the '+' by a '*':
C#
string regexp = @"^[a-zA-Z][\w\s-]*";
 
Share this 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