^.*\.(jpg|jpeg|png|gif|pdf|doc|docx|xls|xlsx|ppt|pptx|txt|zip|rar|tar|gz|bmp|svg)$

Timmy -  
 Timmy -
Hello everyone,

Could you tell me how to compose a regex that would validate files based on their extension, e.g., csv or txt, rtf or doc?

I had this to test the csv, but it doesn't work, and since I'm not skilled in JavaScript...

Thanks in advance

Configuration: Windows XP / Firefox 3.6.17

3 answers

  1. Mihawk Posted messages 4753 Status Contributor 846
     
    Hello,

    Kyser, your code is full of mistakes: bad regex, nonexistent functions, no semicolons, etc... I'm correcting it.

     <script type="text/javascript"> var exempleFichier = "monFichier.txt"; // The 'i' means that case is ignored var regex = new RegExp("\.(csv|txt|rtf|docx?)$","i"); if (exempleFichier.match(regex)){ alert ("Authorized extension"); } else { alert ("Unauthorized extension"); } </script> 

    Best regards,
    Pierre.
    0
    1. Anonymous user
       
      Ok, there are different ways to achieve this, I agree it's not very "clean", but what I provided is to get started with regex, and above all, it works...
      For instantiating a regex, you don't put a semicolon if you declare it like that, the test function exists, the regex is good.

      A small error in the instantiation of exampleFile in your code, there is a ":" instead of an "=".
      0