?
Solved

Check field on submit

Posted on 2005-03-12
6
Medium Priority
?
930 Views
Last Modified: 2008-01-09
hey people

I have this code that runs on form submit

function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" )
        alert("Please press the browse button and pick a file.")
    else
        return true;
    return false;
}

If the field is empty, you get a message (this is an upload form)
I want to keep .exe and .com and .pdf and such of my server so I need to run a check on that field before it's submited. If someone submits an .exe, it gets rejected with a message.

can someone fix the above code for me? I am terrible in javascript.
It would be cool if I can add more extensions later, but for now a check for .exe and .com would do fine...

Thank you very, very much. it is pretty urgent, therefore 500 points..
kindest regards,
Marco
0
Comment
Question by:EnolaKotrotsos
[X]
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
  • 2
  • 2
  • 2
6 Comments
 
LVL 32

Assisted Solution

by:Batalf
Batalf earned 1000 total points
ID: 13525904
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" ){
        alert("Please press the browse button and pick a file.");
        return true;
    }else{
        if(formDOMObj.attach1.value.match(/\.(exe|com|pdf)$/g)){
            alert('Invalid file type');
            return false;
        }
   
    }
       
    return false;
}
0
 
LVL 32

Expert Comment

by:Batalf
ID: 13525912
or better. case insensitive

function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" ){
        alert("Please press the browse button and pick a file.");
        return true;
    }else{
        if(formDOMObj.attach1.value.match(/\.(exe|com|pdf)$/gi)){
            alert('Invalid file type');
            return false;
        }
   
    }
       
    return false;
}
0
 
LVL 32

Accepted Solution

by:
ldbkutty earned 1000 total points
ID: 13525920
function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" ) {
        alert("Please press the browse button and pick a file.")
        return false;
    }
    ext = (formDOMObj.attach1.value).substr(value.lastIndexOf(".")).toLowerCase();
    if (ext == ".exe" || ext == ".com" ext == ".pdf") {
        alert("exe , com and pdf files not allowed.")
        return false;
    }
    return true;
}
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 32

Expert Comment

by:ldbkutty
ID: 13525925
Typo.

 function onSubmitForm() {
    var formDOMObj = document.frmSend;
    if (formDOMObj.attach1.value == "" ) {
        alert("Please press the browse button and pick a file.")
        return false;
    }
    ext = (formDOMObj.attach1.value).substr(value.lastIndexOf(".")).toLowerCase();
    if (ext == ".exe" || ext == ".com" || ext == ".pdf") {
        alert("exe , com and pdf files not allowed.")
        return false;
    }
    return true;
}
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13525980
this checks for multiple blanks as well


<script>
function onSubmitForm(){
    var fileName=document.frmSend.attach1.value;
    if (/^\s*$/.test(fileName)){
      alert("Please press the browse button and pick a file.");
      return false;
    }
    else
    if (/\.(exe|com|pdf)$/.test(fileName)){
      alert('Invalid file type');
      return false;
    }    
    return true;
}
</script>
0
 
LVL 31

Expert Comment

by:GwynforWeb
ID: 13525987
if (/\.(exe|com|pdf)$/i.test(fileName)){

is better as it covers uppercase as well
0

Featured Post

Industry Leaders: 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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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

771 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