OVC-it-guy
asked on
Need Regular Expression that allows letters, numbers, and standard puctuation
I am developing a web site in ASP.NET with VB.NET on MS Visual Web Developer. I have a number of form fields requiring validation. I have regualr expression, "^[a-zA-Z0-9]+(([\'\,\.\!\ - ][a-zA-Z0-9 ])?[a-zA-Z0-9]*)*$", but I need one that allows numbers, letters, dash and standard punctuation (- . , ! ). Can someone please help me with this?
You can also visit following ..
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress.aspx
ASKER
That's a lot more code than what I'm looking for. I just need the validationExpression similar to something like ^[-a-zA-Z0-9.,! ]$ might do it. I was looking for some with more experince on this type of expression.
<asp:RegularExpressionValidator ID="CommentsRegex" runat="server" ControlToValidate="txtComments" ValidationExpression="^[a-zA-Z0-9]+(([\'\,\.\!\- ][a-zA-Z0-9 ])?[a-zA-Z0-9]*)*$" ErrorMessage="Please enter only letters, numbers and punctuation." />
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
To make sure you are matching what you want to match, there's a nice regex tester here that I like to use: http://www.regular-expressions.info/javascriptexample.html
That site also has everything you could possibly want to know about regular expressions if you click around.
That site also has everything you could possibly want to know about regular expressions if you click around.
ASKER
Thanks, petiex.
Then you should use KeyDown and KeyPress event.
So use in following way It will allow only Numbers to be entered in TextBox1
In the same way you can add more checks.
e.g. for letters add check in KeyDown Event.
If e.KeyCode < Keys.A OrElse e.KeyCode > Keys.Z Then
Open in new window