• 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?
0
officerhandsome
Asked:
officerhandsome
2 Solutions
 
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
 
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
Get expert help—faster!

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

 
officerhandsomeAuthor Commented:
Timbo87,

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

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