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
LVL 1
EnolaKotrotsosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.