[Javascript] RegEx for validating a file name

Paul Novitski paul at novitskisoftware.com
Thu May 19 11:01:25 CDT 2005


Rod & Shawn,

If the regexp validates file names generally and isn't dedicated to a 
specific application in which file names are constrained, wouldn't you also 
want to include hyphen & underscore?

^[a-zA-Z0-9_\-]+\.[a-zA-Z]{3}$

Paul


At 08:20 AM 5/19/2005, Shawn Milo wrote:
>You can try this:
>
>^[a-zA-Z0-9]+\.[a-zA-Z]{3}$
>
>There may be a more elegant way to do it, but this should work.
>
>Shawn
>
>
>
>On 5/19/05, Rod Coate <rod.coate at falkirkcollege.ac.uk> wrote:
> > Hi,
> > I am tring to use a regular expression to validate user input (a file 
> name) on my html using javascript.
> >
> > What I have tried so far is:
> > =====================================================
> > <script type="text/javascript">
> > function validateFileName(fileName){
> >         var validReg="\W+\.\W{3}$";
> >         var regex= new RegExp(validReg);
> >         return regex.test(fileName);
> > }
> >
> > function checkFileName(f){
> >         var fileName=f.value;
> >         if(!validateFileName(fileName)){
> >                 alert("file name is not valid");
> >                 f.value="";
> >                 document.myForm.fileName.focus();
> >         }
> > }
> >
> > </script>
> >
> > <form action="" name="myForm" method="post">
> >         <input type="text" name="fileName" value="" 
> onChange="checkFileName(this);">
> > </form>
> > =====================================================
> > but the regex does not seem to work correctly. What I want is to make 
> sure that fileName is alphanumeric, contains only one . and has three 
> alpha characters after the .
> >
> > It also should not contain things like \/*¬| ect or any spaces.
> >
> > Is anyone able to help with the regex to get it to work? (I have tried 
> google, but can't find what I am looking for).
> >
> > Cheers,
> > Rod.
> > _______________________________________________
> > Javascript mailing list
> > Javascript at LaTech.edu
> > https://lists.LaTech.edu/mailman/listinfo/javascript
> >
>
>
>--
>Voicemail any time at:
>206-666-MILO
>_______________________________________________
>Javascript mailing list
>Javascript at LaTech.edu
>https://lists.LaTech.edu/mailman/listinfo/javascript





More information about the Javascript mailing list