How to stop user from entering xml characters

I have a web page that a user can enter in text in a text box.  I want to let the user enter any valid character that does not cause problems when the text is put into xml.

So I want to stop the user from entering in anything that would cause XML to have a problem.  Some of the characters are <, & and so forth.  I don't know what they all are.

If the user enters any of these in, I want to stop them and show an error.  I was wanting to use reqular expressions to do this if possible.

Thanks for your help.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Try searching for these:


Open in new window

If you used a RegularExpressionValidator, then you could do something like:
<asp:RegularExpressionValidator id="RegularExpressionValidator1" 
                                ErrorMessage="Invalid characters."

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RandymarAuthor Commented:
I created a Regular Expression to handle this and added the RegularExpressionValidator to add this to my web page.  I also limited the entry to 50 characters.  And then only allowed the user to use only certain special characters.

The Regular expression I used is :  ^([a-zA-Z0-9@*#!]{1,50})$

Thanks kaufmed for your help, I used your suggestion and fashioned what I wanted.
RandymarAuthor Commented:
Thanks for your help on this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.