The AngularJs directive designed specifically for numerical values is not functioning properly in Internet Explorer 11

I've encountered an issue with an AngularJS directive that is supposed to restrict an input field to only accept numbers. Strangely, the directive functions properly in Chrome and Firefox but fails in IE11.

Does anyone know what needs to be added to fix this problem? Or is it possible that directives simply don't work as intended in IE11? Any assistance in finding a workaround or identifying any errors in the code would be highly appreciated.

The version of IE11 I am using is 11.0.9600.19596 - Update Versions: 11.0.170. In this particular version, I am able to type alphabetical characters into the input box, which differs from the behavior observed in Chrome and Firefox.

I also tested it on IE11 Version 11.1392.17763.0 Update Version 11.0.205

Not sure what else to investigate.

Thank you,

Erasmo

AngularJS for onlynum

app.directive('onlyNum', function () {
        return function (scope, element, attrs) {

            var keyCode = [8, 9, 37, 39, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 110];
            element.bind("keydown", function (event) {
                console.log($.inArray(event.which, keyCode));
                if ($.inArray(event.which, keyCode) == -1) {
                    scope.$apply(function () {
                        scope.$eval(attrs.onlyNum);
                        event.preventDefault();
                    });
                    event.preventDefault();
                }

            });
        };
    });

HTML:

<div class="w-25 p-3">
    <label for="meeting-id">Meeting ID</label>
    <input type="number" ng-model="meetingId" id="meeting-id" ng-trim="true" class="form-control" placeholder="Enter Meeting ID" only-num />
</div>

Answer №1

This code snippet has been tested and works successfully on IE 11 as well as other browsers:

<!DOCTYPE html>
<html ng-app="test">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="71101f16041d10035f1b0231405f455f09">[email protected]</a>" src="https://code.angularjs.org/1.4.8/angular.js" data-semver="1.4.8"></script>
</head>
<body ng-controller="MainCtrl">
    <div class="w-25 p-3">
        <label for="meeting-id">Meeting ID</label>
        <input type="number" ng-model="meetingId" id="meeting-id" ng-trim="true" class="form-control" placeholder="Enter Meeting ID" only-num />
    </div>
    <script>
        var app = angular.module('test', []);

        app.controller('MainCtrl', function ($scope) {
            $scope.name = 'World';
        });

        app.directive('onlyNum', function () {
            return {
                restrict: 'A',
                link: function (scope, elm, attrs, ctrl) {
                    elm.on('keydown', function (event) {
                        if (event.shiftKey) { event.preventDefault(); return false; }
                        if ([8, 13, 27, 37, 38, 39, 40].indexOf(event.which) > -1) {
                            // backspace, enter, escape, arrows
                            return true;
                        } else if (event.which >= 49 && event.which <= 57) {
                            // numbers
                            return true;
                        } else if (event.which >= 96 && event.which <= 105) {
                            // numpad number
                            return true;
                        }
                        else {
                            event.preventDefault();
                            return false;
                        }
                    });
                }
            }
        });
    </script>
</body>
</html>

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

Transferring information in Laravel with the help of Axios and Vue Framework

While following the Laracasts tutorial on integrating Stripe, I noticed that a lot has changed since the release of Laravel 5.4. Although I managed to navigate through most of it, I encountered an issue when trying to submit a payment form using Vue and Ax ...

What is the process of developing a straightforward Node.js script to encapsulate a binary or executable file?

I am looking to develop a Node.js file that can seamlessly execute an executable binary file and handle all inputs and outputs, specifically targeting Windows operating systems. Reason for this: I aim to install an executable tool via npm install -g in or ...

Retrieving the request body in AWS Lambda

Within my AWS Lambda function running on NodeJs 8.0 and receiving requests from API Gateway, the code is structured as follows: const mysql = require('mysql'); exports.handler = (event, context, callback) => { console.log("event.body = " ...

The data in Next.js getStaticProps does not update or refresh as expected

As I recently transitioned to working with Next.js, I have encountered several challenges. One issue that arose was related to the use of useEffect in React compared to its behavior in Next.js. In my index file, I have implemented the following code: impo ...

Convert the PHP datetime and timezone function to a JavaScript function

I have a helpful function in my solution that I'd like to share: public static function formatTime($time, $timezone) { $timezone = new \DateTimeZone($timezone); $time = $time->setTimezone($timezone); return \Locale::getDefaul ...

Which is the better choice: utilizing object literals or constructor functions?

I'm feeling a bit puzzled about the best way to create an object in JavaScript. It appears that there are at least two methods: one involves using object literal notation, while the other utilizes constructor functions. Is there a specific advantage o ...

Obtain the dimensions (width and height) of a collection of images using Angular and TypeScript

Currently, I am facing an issue with my image upload functionality. My goal is to retrieve the width and height of each image before uploading them. However, I've encountered a problem where my function only provides the dimensions for the first image ...

Start by executing the function and then proceed to upload a static file

Here is the code I am working with: var express = require('express'), app = express(); app.use(express.static(__dirname + '/static')); app.get('/', function(req, res) { //??? }); app.listen(80); I need to first ex ...

Emphasize specific lines within a textarea

I am facing a challenge with a textarea dedicated to validating telephone numbers. My goal is to highlight the lines that contain invalid telephone numbers by adding two asterisks in front of them. However, I'm struggling to figure out how to highlig ...

Is it possible to transmit the survey findings through a telegram?

Just finished creating a survey using the platform . Here are the results: Data format: {"question1":["item1"],"question2":["item2"],"question3":["item2"],"question4":["item2" ...

AngularJS, encountering issues with resolving an unknown provider

I have set up two routes with resolve as shown below: .when('/foos', { templateUrl: 'views/foos.html', controller: 'FoosCtrl', resolve: { foo_list: ['$q', '$route', '$timeout', '$locatio ...

Angular Array -- combining numerous data points within a single entity

I'm attempting to build an AngularJS array and apply a filtering function based on the object's name. The challenge I'm facing is trying to filter the array using multiple values within one object name, as shown below. Desired solution: { ...

Transfer information from an Express route to a function exported from a Node module

I'm new to using node.js and I've been told that I need to use middleware, but I'm having trouble understanding its purpose and the proper way to implement it. I have data that is being sent from my view to an express route. ROUTE - route.j ...

The Access-Control-Allow-Headers request header field is not permitted as per the Access-Control-Allow-Headers directive

When trying to send files to my server using a post request, I encountered the following error: Error: Request header field Content-Type is not allowed by Access-Control-Allow-Headers. After researching the issue, I added the necessary headers: $h ...

What is the best way to extract properties from JSON data using Javascript?

The json output provided is as follows: { "categories":[ "Physical support- 18-64 (RA)" ], "series":[ { "name":"Buckinghamshire", "data":[ 9088 ] }, { "name":"Lincolnshire", ...

Troubleshooting Java REST service integration in AngularJS for UPDATE and DELETE operations

After successfully implementing a REST service with Java and testing all HTTP methods using Postman, I decided to explore AngularJS. Upon integrating it to consume the REST service, I encountered issues specifically with the Delete and Put methods not func ...

Experiencing an anonymous condition post onChange event in a file input of type file - ReactJS

When using the input type file to upload images to strapi.io, I noticed that an unnamed state is being generated in the React dev tools. Can someone explain how this happened and how to assign a name to that state? https://i.sstatic.net/ZyYMM.png state c ...

Using a for loop in Javascript with a specified increment value

I am working with an array containing 100 elements. My goal is to extract elements from the 10th to the 15th position (10, 11, 12, 13, 14, 15), then from the 20th to the 25th, followed by the 30th to the 35th, and so on in increments of 4, storing them in ...

Updating a Texture Image in Three.js

I'm currently working on a minecraft texture editor using three.js, with a similar interface to this. My goal is to implement basic click-and-paint functionality, but I'm facing challenges in achieving this. I have textures for each face of every ...

Is there a way to specify the login response details when making an Angular API request?

I am currently using Angular to connect to my backend login service. However, I am facing an issue with setting the popup message when the username or password is incorrect. I want to display the detailed message from the API on my login page when any erro ...