SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot cachobong's Avatar
    Join Date
    Jan 2005
    Location
    cebu
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Restrict File types

    How do i restrict files types that can be accessed when using the input element type file...like the one used when uploading..how do i restrict it to certain files with given file extensions?

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The short answer is you can't. Browsers don't support the X/HTML attribute to do that.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Zealot cachobong's Avatar
    Join Date
    Jan 2005
    Location
    cebu
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    really? omygosh!

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this will work for you:

    1) Add an onsubmit event handler to the submit/upload button that calls a function that you define.

    2) Inside your function, get the value attribute of the file input element, which will be the filename the user entered.

    3) Use substring() and lastIndexOf() to get the extension of the filename the user entered.

    4) Compare the extension to a list of prohibited extensions. If the extension is prohibited, alert() a message to the user, and cancel the submit by returning false from the function. Else, return true from the function, which will cause the submit to proceed and upload the file.

    Note: as with all javascript validation, the user can circumvent your restrictions by disabling javascript.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •