?
Solved

Validate file extension and let an empty field be OK

Posted on 2004-04-06
5
Medium Priority
?
380 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 1000 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

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.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

850 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