Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7933781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:25:52+00:00 2026-06-03T21:25:52+00:00

I take a file via a form , and ask to validate its type.

  • 0

I take a file via a form , and ask to validate its type. but it sounds the java script doesn’t work or the invoke is not seen ?

<form enctype="multipart/form-data" action="Upload2.jsp"
 method="post" onsubmit="javascript:return validate();">
    <td><b>Choose txt file To Upload:</b></td>
    <td><input name="file" type="file"></td>
    <td><input type="submit" value="Upload File" ></td>
</form>

And here’s the script:

<script language="JavaScript">
    function validate(){
        if(document.form.file.value == "" ) {
            alert("Enter course code !");
            return false;}

        else {
            var value = document.form.file.value;
            var ext = value.substr(dot, value.length);
            //ext = value.slice(file.indexOf(".")).toLowerCase();
            if ( ext=="txt" ) {
                return true; }
            else{
                alert("choose a .txt file");
                return false;}
            }
       }
</script>

And this is the form..
tho i am not sure of my script correctness?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T21:25:54+00:00Added an answer on June 3, 2026 at 9:25 pm

    You can capture what’s after the last period in a string using:

    ext = value.split(".").pop();
    

    You take your String value split it into an array:

    var myValue = "some.thing.ext";
    
    var myArray = myValue.split(".");
    

    now myArray is:

    ["some", "thing", "ext"];
    

    You can now pop the last value from the array using .pop().

    var ext = myArray.pop();
    

    now

    ext === "ext";
    myArray === ["some", "thing"]
    

    in context:

    function validate(){
        if(document.form.file.value == "" ) {
            alert("Enter course code !");
            return false;}
    
        else {
            var value = document.form.file.value,
                ext = value.split(".").pop();
            if ( ext == "txt" ) {
                return true; }
            else{
                alert("choose a .txt file");
                return false;}
            }
       }
    

    If you change your onsubmit to:

    onsubmit="javascript:return validate(this);"
    

    Then you will get the form element as first argument in value:

    function validate(form){
        var value = form.file.value,
            ext = value.split(".").pop();
    
        if( !value ) {
            alert("Enter course code !");
        }
        else if ( ext !== "txt" ) {
            alert("choose a .txt file");
        }
    
        return ( ext === "txt" );
    }
    

    NOTE:

    Never use javaScript as your only validation. Use some sort of server-site language to determinate if the validation is passed.

    Use JS to make i more slick for the user to operate on your site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET page with a cs script file to take in data
I am trying to upload a file via a form and then save in
The program is supposed to take a file, say data.dat, filled with a list
I'm writing a Windows Service that needs to take a file from an FTP
If I set a far future expire header for this file (take note of
Take this mapping file <class name=com.data.StudentData table=gr_student> <id name=id> <generator class=identity /> </id> <property
I want to take an XML file and replace an element's value. For example
I'm writting a function to take an invoice file in a defined format and
I was volunteered to write a application that will take a mp3 file, upload
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.