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 9147665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:03:03+00:00 2026-06-17T11:03:03+00:00

I am trying to upload a file . and validating it using this code

  • 0

I am trying to upload a file . and validating it using this code

$(document).ready(function () {
  $('#form_model_upload').submit(function (e) {
    var isvalid = true;
    var val = $('#model').val();
    if (!val || val == "") {
      $('#err_model').html('please select an image');
      isvalid = false;
    } else {
      var ext = $('#model').val().split('.').pop().toLowerCase();
      if ($.inArray(ext, ['gif', 'jpg', 'jpeg']) == -1) {
        $('#err_model').html('invalid file extension').show();
        isvalid = false;
      }
    }
    return isvalid;
  });
});

here is my html code

 <form action="<?php echo $editFormAction; ?>" method="POST" name="form_model_upload">
        <input type="hidden" name="vendor_id" value="<?php echo $_SESSION['vendor_id']?>" /><tr><td></td><td><span id="err_model"></span></td></tr>
        <tr><td>Model:</td><td><input type="file" name="model" id="model"/></td>
        </tr>

        <tr><td>Description:</td><td><textarea rows="5" cols="30" name="description" id="description"  ></textarea></td></tr>
        <tr><td>Category:</td><td><select name="category_model" id="category_model">
        <?php $categories=$doc->getElementsByTagName("category");
        foreach($categories as $category)
        {//$category_node=$category->getElementsByTagName("cat_val");
        $category_value=$category->nodeValue;
        echo '<option value="'.$category_value.'">'.$category_value.'</option>';
        }?>

        </select> </td></tr>
        <tr><td></td><td><input type="submit" name="upload_submit" id="upload_submit" value="upload" /></td></tr>
        <input type="hidden" name="MM_insert" value="upload_model" />
        <input type="hidden" name="MM_insert" value="form_model_upload" />
       </form>

but it submits the form even if the validation fails??
i want to prevent the form submit on validation failed.

  • 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-17T11:03:04+00:00Added an answer on June 17, 2026 at 11:03 am
    $(document).ready(function () {
      // move this var outside of submit
      var isvalid = false;
      $('#form_model_upload').submit(function (e) {
        // block submit if false (it will be the first time)
        if(!isvalid){ e.preventDefault(); }
        // set true state here before the validation test
        isvalid = true;
        var val = $('#model').val();
        if (!val || val == "") {
          $('#err_model').html('please select an image');
          isvalid = false;
        } else {
          var ext = $('#model').val().split('.').pop().toLowerCase();
          if ($.inArray(ext, ['gif', 'jpg', 'jpeg']) == -1) {
            $('#err_model').html('invalid file extension').show();
            isvalid = false;
          }
        }
        // if form is valid, submit form
        // isvalid is now true so e.preventDefault() wont fire
        if(isvalid){ 
            $(this).submit(); 
        }
      });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to upload a file to an FTP server using code based on
I am trying to upload file to rackspace cloud file using the following code:
I am trying to upload a file using jQuery ajax, but keep getting this
I'm trying to upload file, using XMLHTTPRequest, and sending this headers: Content-Type:multipart/form-data, boundary=xxxxxxxxx --xxxxxxxxx
I am trying to upload file using php. I am using the function move_uploaded_file.
I am trying to upload file using dropbox api but following Code shows some
I am trying to upload file to an sharepoint online server using webclient. Up
I was trying to upload file(s) using PrototypeJs request method but I failed. The
I'm trying to upload a file using Watin and C#. Under Windows XP, Watin
I am trying to upload a file using commons-fileupload-1.1.1.jar file (I know it is

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.