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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:16:35+00:00 2026-05-29T06:16:35+00:00

var $imagefile = $(‘<input />’) .attr({ type: ‘file’, name: ‘imageFile’, id: ‘imageFile’ }); Above

  • 0
var $imagefile = $('<input />')
    .attr({
        type: 'file',
        name: 'imageFile',
        id: 'imageFile'
    });

Above is my code which creates a file input.

Below is the code which checks if a file format is correct in a table row:

function validation() {

    var marks = parseInt($("#total-weight").text());    
    var _qid = "";
    var _msg = "";


    var allowedTypes = ["jpg", "jpeg", "gif", "png"];

    var path = $("#imageFile").val();
    var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();

    var alertValidation = "";
    // Note, this is just so it's declared...
    $("tr.optionAndAnswer").each(function() {


        _qid = $("td.qid",this).text();
        _msg = "You have errors on Question Number: " + _qid + "\n";


        $("#imageFile",this).each(function() {

            if ($.inArray(ext, allowedTypes) < 0) {
                alertValidation += '\n\u2022 Unsupported file type';
            }

            if (alertValidation != "") {
                return false; //Stop the each loop 
            }
        });

        if(alertValidation != ""){
            return false;
        }
    });


    if (alertValidation != "") {
        alert(_msg + alertValidation);
        return false;
    }

    return true;
}

Now this works fine by recognising which file type is correct and incorrect. The problem is though that if in all table rows the user does not select a file, then it comes up with the alert which is incorrect.

How can I get it so that it also allows a blank file input where the user does not select a file?

  • 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-05-29T06:16:35+00:00Added an answer on May 29, 2026 at 6:16 am

    In this routine

    $("#imageFile",this).each(function() {
    
            if ($.inArray(ext, allowedTypes) < 0) {
                alertValidation += '\n\u2022 Unsupported file type';
            }
    
            if (alertValidation != "") {
                return false; //Stop the each loop 
            }
        });
    

    You should check to see if it has a value, and if so, continue processing. Something like this:

    $("#imageFile",this).each(function() {
        if($(this).val().length) {
            // continue on
        }
    });
    

    After our comments, you should probably be using a class selector instead of an id selector. So your creation code should probably look like:

    var $imagefile = $('<input />')
    .attr({
        type: 'file',
        name: 'imageFile',
        class: 'imageFile'
    });
    

    And your routine should be:

    $(".imageFile",this).each(...
    

    That way you can iterate over a collection.

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

Sidebar

Related Questions

<html> <body> <form name=Upload enctype=multipart/form-data method=post> Filename: <INPUT type=file id=submit> <INPUT type=button id=send value=Upload>
var query1 = urlencode($('input[name=searchTerm1]').val()); //user1 var query2 = urlencode($('input[name=searchTerm2]').val()); //user1 var rpp = 20;
var query1 = urlencode($('input[name=searchTerm]').val()); //user1 $.getJSON('http://twitter.com/friends/ids.json?screen_name='+ query1 +'&jsoncallback=?', function(data){ $('content').append(''+data.length+''); }); This is a
Below I have a jquery code which includes a textbox where the user can
var i : integer; i := 1234567; Given the above, I want the string
var htm = $.ajax({ type: GET, url: ./viewforms, async: false }).responseText; var myObject =
CONTROLLER class ImagesController extends AppController { var $name = 'Images'; var $uses = array('Image','Person');
I have image file which is 6k jpg file with width: 172px and hight:
I am using the following c# code to convert an image file to a
Here's my nonworking attempt: <script> function uploadImageSubmit() { var imageFile = $('.imageFile').val(); $.ajax({ url:

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.