faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

42 of 71 people (59%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

how to check the file extension using javascript

Apr 8th, 2008 18:49
ha mo, Dave Clark, bilal ahamd,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Bilal,
     You can check it either when the field is changed or when the 
form is submitted.  Just keep in mind that if JavaScript is disabled 
then this code won't perform any client-side validation for you.  Here 
is the function:
var valid_extensions = /(.jpg|.jpeg|.gif)$/i;
//
function CheckExtension(fld)
{
    if (valid_extensions.test(fld.value)) return true;
    alert('The selected file is of the wrong type.');
    fld.select();
    fld.focus();
    return false;
}
    Then, you can call it as follows:
<form onsubmit="return CheckExtension(this.elements['fileupload'])">
  <fieldset>
    <input type="file" name="fileupload" size="40"
      onchange="return CheckExtension(this)">
  </fieldset>
</form>
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com