Solved

Validate file extension and let an empty field be OK

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

730 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