Solved

Need Regular Expression that allows letters, numbers, and standard puctuation

Posted on 2008-10-19
6
1,301 Views
Last Modified: 2009-07-29
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?
0
Comment
Question by:OVC-it-guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Muhammad Kashif
ID: 22755158
What I understood that you want that your textbox should allow numbers, letters, dash and ... Not the regular expression with specific format.

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

Public EnteredText As Boolean
 
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    EnteredText = False
    If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
        If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
            If e.KeyCode <> Keys.Back And e.KeyCode <> Keys.Subtract Then
                EnteredText = True
            End If
        End If
    End If
End Sub
 
 
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    If EnteredText = True Then
        e.Handled = True
        EnteredText = False
    End If
End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:Muhammad Kashif
ID: 22755171
0
 

Author Comment

by:OVC-it-guy
ID: 22755193
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." />

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 11

Accepted Solution

by:
petiex earned 500 total points
ID: 22755376
^[a-zA-Z0-9\'\,\.\!\-\?]*$
Will match zero or more numbers, characters, and the following characters
' , . ! - ?

If you want it also to match space characters, put a space somewhere between the brackets:
^[a-zA-Z0-9\'\,\.\!\-\? ]*$

To match one or more characters, use + instead of *:
^[a-zA-Z0-9\'\,\.\!\-\? ]+$
0
 
LVL 11

Expert Comment

by:petiex
ID: 22755385
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.
0
 

Author Comment

by:OVC-it-guy
ID: 22755553
Thanks, petiex.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ASP.NET Load html part first, then start function 2 40
history of pages 7 53
Run software updates from the website 6 50
Convert a string to date 4 40
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question