• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 704
  • Last Modified:

Validation for Text Box to prevent commas and periods.

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?
2 Solutions
Use a RegularExpressionValidator with this regular expression:


This will only allow letters, numbers, and spaces (no punctuation).
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?
Ignacio Soler GarciaSolution Architech & Technical LeadCommented:
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.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

officerhandsomeAuthor Commented:

How about if I also didn't want spaces?  Then what would that validation statement look like?
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:

officerhandsomeAuthor Commented:
Perfect, Thanks.  Can anyone recommend a good source to learn how to write these validation expressions?
Google: Regular Expressions


This looks like a decent place to start: http://etext.lib.virginia.edu/helpsheets/regex.html

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now