Javascript Regex filename validation

I am trying to weed out 'bad' characters when a user is creating a filename. I am doing so with Javascript. Below is my code, another set of eyes would be great.

I am always getting my else condition "This is an invalid..."

Thanks.
<form enctype='multipart/form-data' action='web_query2.php' method=POST name='web_query' onsubmit=\"return blah(document.getElementById('txt').value);\">
 
 
function blah(str) {
   if (/^[^\\\/\:\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/.test(str)) {
      alert(\"valid file name\");
      return true;
   }
   else {
     alert(\"This is an invalid filename, please try again!\");
     return false;
   }
}

Open in new window

Neil2526Asked:
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.

humanonomicsCommented:
I tried it using the attached code snippet and your method works like cool for me
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
 
<SCRIPT LANGUAGE='JAVASCRIPT'>
function blah(str) {
   if (/^[^\\\/\:\*\?\"\<\>\|\.]+(\.[^\\\/\:\*\?\"\<\>\|\.]+)+$/.test(str)) {
      alert("valid file name");
      return true;
   }
   else {
     alert("This is an invalid filename, please try again!");
     return false;
   }
}
</SCRIPT>
<BODY>
<form enctype='multipart/form-data' action='web_query2.php' method=POST name='web_query' >
 
<input type="button" name='submit' onclick='blah("abc.txt")'>
 
</form>
 
 
</BODY>
</HTML>

Open in new window

David S.Commented:
Do you have a particular reason for not allowing multiple extensions? "archive.tar.gz" is an example of one.
Neil2526Author Commented:
Of course. The user is creating a csv data file.  
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

humanonomicsCommented:
so why not also check for .csv ?
Neil2526Author Commented:
they are not creating the extension, only the filename.

ie:
       <th align=\"center\" class=\"fields\">
         <input type=\"text\" id=\"txt\" name=\"filename\">.csv
       </th>
Neil2526Author Commented:
human -

I tried your snipit, too and it works fine. I am still not hitting my first condition. I am always hitting my else clause. Attached is the full php file.
file.txt
humanonomicsCommented:
Oww ok, so the input is only the file name without extension ?
Neil2526Author Commented:
yes. i think it has something to do with my regex.
humanonomicsCommented:
But I used the same regex that you have used.
Neil2526Author Commented:
the regex checks for:

XXX.XXX

My form submits:

XXX

ie:
       <th align=\"center\" class=\"fields\">
         <input type=\"text\" id=\"txt\" name=\"filename\">.csv
       </th>

The user does not put in the DOT and the file extension.
humanonomicsCommented:
Try this regex:: (/^[^\\\/\:\*\?\"\<\>\|\.]+$/.test(str))

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
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
JavaScript

From novice to tech pro — start learning today.