Solved

Validate file extension and let an empty field be OK

Posted on 2004-04-06
5
374 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
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-…
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…

813 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

11 Experts available now in Live!

Get 1:1 Help Now