We help IT Professionals succeed at work.
Get Started

javascript check string for bad characters

stepfar88 asked
Last Modified: 2007-12-19
I feel like a real idiot.  I'm still new with JavaScript, and RegExp.  I need to validate file names in a form before users upload them to our server with names that http can't handle displaying.  I've got ASP doing the upload on the server side.
I have written the following code, but it won't work for ampersands.
How can I validate to alert the user to not put ampersands, too?
Here's my code for apostrophes and semicolons.
(This code is really lame, because I need to learn RegExp.)

//File name must have valid characters.
if (theForm.file1.value != "") {
    var str = (theForm.file1.value)
    var semicolon = str.indexOf(";")
    if (semicolon >= 0){
        alert("The file name cannot contain semicolons.\nPlease rename this file before uploading to the Repository.");
     return false;
    var apostrophe = str.indexOf("'")
    if (apostrophe >= 0){
        alert("The file name cannot contain apostrophes.\nPlease rename this file before uploading to the Repository.");
     return false;
return true;

Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE