Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 939
  • Last Modified:

Check field on submit

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
EnolaKotrotsos
Asked:
EnolaKotrotsos
  • 2
  • 2
  • 2
2 Solutions
 
BatalfCommented:
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
 
BatalfCommented:
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
 
ldbkuttyCommented:
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
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!

 
ldbkuttyCommented:
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
 
GwynforWebCommented:
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
 
GwynforWebCommented:
if (/\.(exe|com|pdf)$/i.test(fileName)){

is better as it covers uppercase as well
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now