Tips for validating an excel file using AngularJS

I am working on developing a file upload feature and I'm in need of validation specifically for Excel files. The structure of my form is as follows:

<div class="row">
    <input type="hidden" name="key" value="uploads/${filename}">
        <table class="content-centered">
           <tr>
              <td><input type="file" file-model="imageToUpload" data-rule-required="true"
                 accept="application/vnd.ms-excel" /></td>
              <td><span ng-class="{errorMessage: fileStatus.status == false,
              regularMessage: fileStatus.status == true}"> &nbsp{{fileStatus.message}}
               </span>
              </td>
            </tr>
           </table>

        <div class="centered" style="margin-top: 12px;">
            <div ng-click='imageUpload()' class="btn blueBtn">Upload &nbsp;&nbsp;
                <span class="glyphicon glyphicon-upload"></span>
            </div>
            <div ng-click='$close()' class="btn orgBtn">Close</div>
        </div>
    </div>

I have tried using data-rule-required="true" and accept="application/vnd.ms-excel" for validation, but unfortunately it did not work as expected. Does anyone have insights on how file validation works in AngularJS?

Answer №1

Validating an excel file in angular or javascript does not have a built-in function. I achieved this by utilizing extensions of the specific file:

$scope.fileToUpload.name='uploadSchoolFeeData.xls';

Subsequently, we can validate it by checking:

if (($scope.fileToUpload.name.substring($scope.fileToUpload.name.lastIndexOf('.') + 1) != 'xls') && ($scope.fileToUpload.name.substring($scope.fileToUpload.name.lastIndexOf('.') + 1) != 'xlsx')) {
$scope.errorMsg='please upload valid excel file';
}

This method allows us to effectively validate all types of files.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Assistance in using jQuery to locate specific div elements is

I am currently working on creating a navigation bar that features icons triggering contextual submenus upon hover. The main idea is that hovering over an icon will display a popup menu or tooltip with additional options, while still allowing the icon itsel ...

Is it possible to input tab characters in a TextField?

I am working with a multi-line MUI TextField and my objective is to input JSON text. However, I encountered an issue where pressing the tab key causes the component to lose focus and shift to the next element on the page. Is there a way to prevent the ta ...

Unleashing the Power of Node Js: Retrieving File Data and Form Data in POST Requests

When sending form data values using Postman, you can utilize a NodeJS server in the backend to handle the POST request. Here is an example of how to structure your code: require('dotenv').config(); const express = require('express'); co ...

Importing a third-party JavaScript library in Angular 2 RC5: Utilizing Showdown Library

Struggling with the integration of Showdown as a vendor in my project. Whenever I compile, the browser console throws an error saying showdown is not defined. Since it's a vendor package, importing it directly into app.module.ts doesn't seem to b ...

Why is the content overlapping the slider once it ends?

I have been working on a slider that functions perfectly, however, I am facing an issue where the content of the web-page is overlapping with slides 2-3 of the slider. I do not want to set a fixed height for the slider but still want the content after the ...

When attempting to update a task with the Microsoft Graph API, the server returned a 400 error code

Attempting to update a Task using the Graph API from an angular2 typescript app. Here is an example of my request - Request URL:https://graph.microsoft.com/beta/tasks/_1EKXXuN1UGInJ9yVHAjIpYAKuC2 Request Method:PATCH Request Body: { "createdBy":"f16 ...

Discover how to extract the value from the server side during document.ready using jQuery without the need for any specific event

In my project, I'm facing a requirement where I need to fetch a value from server-side code using any event and utilize it to create a dialog box. Initially, I attempted to retrieve the value in window.onload but discovered that window.onload is trigg ...

Stopping free jqgrid disabled toolbar buttons from reacting to mouse clicks can be achieved by implementing specific coding techniques that

When using Free jqgrid, toolbar disabled buttons may trigger click events on mouse clicks which can lead to invalid code execution. To demonstrate this, open the page below in Chrome and click on a disabled inline edit or pager button. A rectangle will app ...

Should I use React with Spring Boot or just Spring Boot for this project?

My partner and I are collaborating on a project for our current semester course. As we delve into our research on potential technologies to use, it seems like Spring Boot for the server side along with MySQL or Postgres for the database are emerging as s ...

Prevent Cross-Site Scripting attacks in Laravel by sanitizing user input, even when allowing HTML tags

What measures can be taken to protect against XSS attacks when a user is allowed to use HTML tags, such as in a textarea element? Avoiding the stripping or escaping of all tags complicates the situation and rules out the option of using {{ }}. It's a ...

Issue with process.env.NODE_ENV not functioning appropriately in NodeJS when utilizing package.json scripts

I currently have three separate databases configured for testing, development, and production purposes. My goal now is to make my express app switch between these databases based on the script that is being executed. These are the scripts I am using: "s ...

Searching for specific tags using PHP and JavaScript in MySQL: effective strategies to find what you're looking

What is the most efficient way to search for an item based on user-defined tags? Consider the following data: item | param ----------------- 1 | a 1 | b 1 | c 2 | b 2 | c 2 | d 3 | c 3 | d 3 ...

Uploading files in chunks: a guide to storing files in Azure blob storage using ng-file-upload

I'm currently developing an Angular application that requires the functionality for users to upload multiple files to an Azure blob storage container using ng-file-upload and HTML5. I've already gone through the documentation provided at https:// ...

Implementing Vuejs Array Push in Separate Components

I am attempting to extract data from an object and store it in an array using Vue. My goal is to have each value stored in a separate array every time I click on an item. Each click should push the todo into a different array. How can I achieve this separa ...

Utilizing Asynchronous Values in a Different JavaScript Function

Currently delving into the world of destructuring arrays in Javascript, I find myself faced with the challenge of accessing these variables in other functions. I attempted to retrieve the array by calling a function and then using console.log(thermostatAr ...

Animation of hand-drawn letters using SVG technology

I'm exploring SVG animations and am currently struggling with animating a slogan letter by letter. The font is handwritten and should give the effect of being written with a text marker. Can anyone provide me with some tips on how to approach this cha ...

The JavaScript audio is not working for the first three clicks, but starts playing smoothly from the fourth click onwards. What could be causing this issue?

$(".btn").click(function(event){ playSound(event.target.id); }); function playSound(name){ $("." + name).click(function() { new Audio("sounds/" + name + ".mp3").play(); ...

Cookie Cutter Chrome Extensions

Is there a way to create a cookie that can be shared between my plugin and contentPage? I've tried creating one but it doesn't seem to appear on the cookie list of the tab page. Any suggestions or ideas on how to achieve this? ...

Developing a quiz using jQuery to load and save quiz options

code: http://jsfiddle.net/HB8h9/7/ <div id="tab-2" class="tab-content"> <label for="tfq" title="Enter a true or false question"> Add a Multiple Choice Question </label> <br /> <textarea name ...

Organize the JSON data in a particular manner

I have a set of JSON data that looks like this: [ { "name": "Event 1", "sponsors": [ { "name": "Walmart", "location": "Seattle" }, { "name": "Target", "location": "Portland" }, { ...