Solved

Validate file extension and let an empty field be OK

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…
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…

758 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

22 Experts available now in Live!

Get 1:1 Help Now