Validation for Text Box to prevent commas and periods.

Posted on 2004-11-01
Last Modified: 2011-04-14
I have a webform that has several text boxes.  When users enter information, such as their first name, middle name, and last name into the textboxes I need to prevent any periods or commas from being passed through to my SQL DB with the name.  For instance:

John D Smith will work fine.  

But, John D. Smith will not work.

Is there some way to validate that text entered either by a validator in Visual Studio, or some way that I could code that into my save button?
Question by:officerhandsome
    LVL 15

    Assisted Solution

    Use a RegularExpressionValidator with this regular expression:


    This will only allow letters, numbers, and spaces (no punctuation).
    LVL 14

    Expert Comment

    using javascript you could create a regular expression that will only allow the characters you want entered into the text box ... or you could just strip them out on the server side ... or using the Regular Expression Validator control ... that should work.

    What characters do you want to allow ... only letters and spaces?
    LVL 5

    Expert Comment

    by:Ignacio Soler Garcia
    You can also try the nice Vb.Net component ErrorProvider (System.Forms.Component.ErrorProvider) It's very nice and informs the user when the error is done not at the end of the typing.


    Author Comment


    How about if I also didn't want spaces?  Then what would that validation statement look like?
    LVL 14

    Accepted Solution

    should be ^[\w\d]+$

    the [] - brackets contain what to look for and the \w matches any alphanumeric and the \d matches any decimal digit. The \s matches whitespace characters

    on second thought you might be able to get away with just:


    Author Comment

    Perfect, Thanks.  Can anyone recommend a good source to learn how to write these validation expressions?
    LVL 14

    Expert Comment

    Google: Regular Expressions

    This looks like a decent place to start:

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: From Zero to Hero with Nodejs & MongoDB

    Interested in Node.js, but don't know where to start or how to learn it properly? Confused about how the MEAN stack pieces of MongoDB, Expressjs, Angularjs, and Nodejs fit together? Or how it's even possible to run JavaScript outside of the browser?

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    884 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now