How do i do a regex for file extension and file path form validation to use in javascript

Posted on 2009-02-12
Last Modified: 2012-05-06
does anyone know how to make file extension validation regex to use with javascript note: my users upload photo from their harddisk so we need to validate the path

Thank you
Question by:MrFahad
    LVL 15

    Assisted Solution

    could you please assign more points?
    <form onsubmit="return validateHeight(this);">
    <input type="text" name="imagename">
    <input type="submit" value="validate extension">
    <script language="javascript">
    var pattern=new RegExp("\.(gif|jpg)$");
    function validateHeight(formObj){
    //  alert(pattern.test(formObj.imagename.value));
      return (pattern.test(formObj.imagename.value));

    Open in new window

    LVL 18

    Accepted Solution

    What are the allowable extentions? JPG, GIF, PNG, BMP?  I'll assume all of them.

    <form action="whatever" onsubmit="return validateExt(this)" enctype="multipart/form-data">
    ...all your form stuff.
    <input type="file" name="file" id="file" />

    //place this between your script tags
    function validateExt(formToValidate)
         return false;
      //do whatever else here
      return true;
    //also test on your server side in case javascript is disabled!

    Open in new window


    Author Closing Comment

    i used Morcalavin solution but bot solutions work great
    thanks for the help guys :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Hire Freelancers to Complete JavaScript Projects

    Source the talented Expert Exchange community
    for top quality work on your JavaScript projects.

    Hire the best. Collaborate easily. Get quality work.

    Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
    Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now