Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Validate file extension and let an empty field be OK

Posted on 2004-04-06
5
375 Views
Last Modified: 2011-09-20
Hi,
i use the following to validate wether the file for upload is in a valid format. I would like the script to allow an empty field also.
--------------------
function ValFileField(entered, alertbox) {
   with (entered){
      ext=value.substr(value.lastIndexOf(".")).toLowerCase();

      if (ext!=".jpg"&&ext!=".gif"&&ext!=".bmp"&&ext!=".png"){
         if (alertbox!="") {
           alert(alertbox);
        }
        return false;
    }
    else {
      return true;
    }
  }
}
------------------------------
0
Comment
Question by:jenmat
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10764359

function ValFileField(entered, alertbox)
{
  if (entered.value=="") return true;

  with (entered){
      ext=value.substr(value.lastIndexOf(".")).toLowerCase();

      if (ext!=".jpg"&&ext!=".gif"&&ext!=".bmp"&&ext!=".png"){
         if (alertbox!="") {
           alert(alertbox);
        }
        return false;
    }
    else {
      return true;
    }
  }
}
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 250 total points
ID: 10764492
Or like this:

function ValFileField(entered, alertbox) {
  with (entered){
     ext=value.substr(value.lastIndexOf(".")).toLowerCase();

     if (ext!=""&&ext!=".jpg"&&ext!=".gif"&&ext!=".bmp"&&ext!=".png"){
        if (alertbox!="") {
          alert(alertbox);
       }
       return false;
   }
   else {
     return true;
   }
 }
}

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 10764809
Thanks for points.

But next time please consider to split the points to all experts who helped you.

See you,
Zvonko
0
 

Author Comment

by:jenmat
ID: 10764836
Will do, just thought your solution were better because it had less code.

venkateshwarr, i apologies / Jens
0
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10776304

Thats ok..
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

840 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