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?
officerhandsomeAsked:
Who is Participating?
 
dfu23Commented:
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:

^[\w]+$
0
 
Timbo87Commented:
Use a RegularExpressionValidator with this regular expression:

^[\w\d\s]+$

This will only allow letters, numbers, and spaces (no punctuation).
0
 
dfu23Commented:
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?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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.

SoMoS
0
 
officerhandsomeAuthor Commented:
Timbo87,

How about if I also didn't want spaces?  Then what would that validation statement look like?
0
 
officerhandsomeAuthor Commented:
Perfect, Thanks.  Can anyone recommend a good source to learn how to write these validation expressions?
0
 
dfu23Commented:
Google: Regular Expressions

http://www.google.com/search?q=regular+expressions

This looks like a decent place to start: http://etext.lib.virginia.edu/helpsheets/regex.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.