Personalizing a directive to prevent users from entering special characters in AngularJS

I am currently diving deep into the world of AngularJS directives. My current project involves using a directive to prevent users from inputting special characters.

Below is the code snippet:

HTML

<input type="text" no-special-char ng-model="vm.customTag" class="form-control" value="" />

AngularJS Directive

app.directive('noSpecialChar', function () {
    return {
        require: 'ngModel',
        restrict: 'A',
        link: function (scope, element, attrs, modelCtrl) {
            modelCtrl.$parsers.push(function (inputValue) {
                if (inputValue == null)
                    return ''
                cleanInputValue = inputValue.replace(/[^\w\s]/gi, '');
                if (cleanInputValue != inputValue) {
                    modelCtrl.$setViewValue(cleanInputValue);
                    modelCtrl.$render();
                }
                return cleanInputValue;
            });
        }
    }
});

I have two requirements:

(1) I want to allow users to input a minus/dash '-' character, which is currently not permitted by my regex /[^\w\s]/gi. How can I modify it to allow the minus sign?

(2) The existing functionality only restricts special characters, but I also want to display a red alert message saying "special characters are not allowed" when a user attempts to input one. How can I achieve this additional feature?

Any guidance or assistance on these matters would be greatly appreciated!

Thank you

Answer №1

Query #1

Your current regular expression is selecting every character that is not a word character or whitespace. If you want to include a hyphen, you need to add it to the list of characters not to be replaced.

Try using this updated regular expression:

/[^\w\s-]/gi

If there are any other characters you want to exclude from replacement, just append them after the hyphen.

See Live Demo on RegExr


Query #2

To track changes in your form content, listen for the keypress event and use .match() to detect special characters. This method will return a truthy string if special characters are found, otherwise false.

You can evaluate the result of .match() within an if statement and show an alert message when needed.

if (inputValue.match(/[^\w\s]/gi)) {
    // Display Alert Here!
}

Alternatively, you can implement the alert creation within the

if (cleanInputValue != inputValue)
condition. This approach achieves similar results as using .match(), detecting alterations made by .replace() and executing the code inside the if block accordingly.

Answer №2

Following feedback and comments:

This solution provides the functionality you requested, albeit in a non-conventional "Angular" way - it still accomplishes the desired outcome.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="ns.test">
<head>
    <title>Test</title>
    <!-- http://stackoverflow.com/questions/36303590/customizing-directive-to-restrict-the-user-to-input-special-characters-angular -->

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>

    <style>
        .input {
            padding: 20px;
            margin: 50px;
        }

            .input input {
                border: solid 1px black;
                font-family: Tahoma;
                font-size: larger;
                padding: 5px;
            }

        .err {
            color: red;
            padding: 5px;
            margin: 10px;
            font-family: Tahoma;
            font-size: larger;
            display:inline;
        }
    </style>

    <script type="text/javascript">

        var app = angular.module('ns.test', []);

        app.controller('testController', function ($scope) {
            $scope.item = 'item1';
            $scope.item2 = 'item2';
            $scope.item3 = 'item3';
            $('input:first').focus();
        });

        var noSpecialChar = function () {
            return {
                restrict: 'A',
                require: '?ngModel',
                link: function (scope, elm, attr, modelCtrl) {
                    if (!modelCtrl) return;

                    modelCtrl.$parsers.push(function (inputValue) {
                        if (inputValue == null)
                            return ''

                        var parent = $(elm).parent();

                        cleanInputValue = inputValue.replace(/[^a-z0-9-_]+/gi, '');
                        if (cleanInputValue != inputValue) {
                            if (parent.find(".err").length == 0)
                                parent.append("<div class=\"err\">Invalid Characters</div>"); // change the class here to your bootstrap alert classes
                        } else
                            parent.find(".err").remove();

                        return cleanInputValue;
                    });
                }
            };
        };

        app.directive('noSpecialChar', noSpecialChar);

    </script>
</head>
<body data-ng-controller="testController">
    <form name="userForm" novalidate>
        <div class="input">
            <input type="text" data-no-special-char data-ng-model="item" />
        </div>
        <div class="input">
            <input type="text" data-no-special-char data-ng-model="item2" />
        </div>
        <div class="input">
            <input type="text" data-no-special-char data-ng-model="item3" />
        </div>
    </form>
</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

What could be causing the Toast message to not show up in react-native-root-toast?

Incorporated react-native-root-toast into my expo project running on expo 51. Please see the code snippet below for reference: const toastColors = { 'error': { color: '#DA5C53', iconName: <WarningIcon size="5 ...

Unable to retrieve the variable within the event handler

Currently, I am working on managing a simple form using JQuery for DOM manipulation. Below is the code snippet: var username = $('#username').val(); var email = $('#email').val(); var pass = $('#pass').val(); $('#signup ...

Issues with JavaScript Array Splice Function

var cache = []; cache[0] = "0"; cache[1] = "1"; cache[2] = "2"; cache[3] = "3"; cache[4] = "4"; cache["r"] = "r"; console.log(cache.length); for(key in cache){ if(isNaN(key))continue; else cache.splice(key,1); // The functionality of cache.splice(k ...

How to position items at specific coordinates in a dropdown using JavaScript

I have five images in my code and I would like to arrange them in a circular pattern when they are dropped into the designated area. For example, instead of lining up the five images in a straight line, I want them to form a circle shape once dropped. Ho ...

Error: $this.text is throwing a TypeError and is not working as a function

Upon examining the code below: var $this = $(this).children('div.submenu1').children('a.subtile')[0], title = $this.text(), name = $this.attr('node').val; An error is encountered: Uncaught TypeError: $this.text is not a fun ...

What is the root cause behind the recurring appearance of this line in Angular/D3.js?

I recently came across an excellent tutorial on integrating the D3.js library with AngularJS by following this link: . The guide provided has been extremely helpful in getting me started (big thanks to Brian!) However, I'm eager to delve deeper into ...

What is the process for utilizing "yarn create <pkg-name>" command?

While browsing the Yarn blog, I came across a feature that caught my attention - yarn create. This function is similar to create-react-app. https://yarnpkg.com/blog/2017/05/12/introducing-yarn/ I decided to give it a try on my local machine by creating a ...

Submit image on Laravel platform from canvas

My webpage has a canvas image that is generated using JavaScript, and I am attempting to upload it to my server using Laravel. Below is the code in my controller: public function handleImageUpload(Request $request) { $imageName = time().' ...

Validator returns undefined when expressing invalid data

Having an issue with validation, here is the code snippet: routes.js var express = require('express'); var router = express.Router(); var hello_controller = require('../api/controllers/helloController'); var { validationRules, validat ...

The filtering feature of ng-grid doesn't seem to function properly in Internet Explorer 8 when using blank

When I assign filterOptions.filterText = 'category : ' + filteringText, no rows are displayed when filteringText is empty. However, if filteringText is not empty, then the filter works correctly. You can see a preview in this link This issue is ...

Prestashop: Eliminate user uploaded files with AJAX for seamless experience

By default, a user uploaded image will display with a small black X indicating it can be deleted: <a href="path/to/yourproduct?deletePicture=1" title="Delete" > When the user clicks on this button, the page 'reloads' and the uploaded file ...

Incorporate live Twitch streaming onto a web platform

I'm currently in the process of making a website for a friends twitch stream and I'm very confused as to how I implement the twitch stream. I have created a div with the class "Twitchscreen" but I have no idea how I link to the twitch API or get ...

Issue encountered while attempting to utilize the concat method to condense an array

Describing the Problem: I am facing a challenge with flattening an array of sales data. Each element in the array contains an ID, sale code, seller username, timestamp, and details which include an array of products, quantities, and subtotals for each item ...

Unable to access the attributes of the mongoose model

I'm experiencing an issue with my mongoose model.find code below Displayed here is my node.js code that utilizes mongoose to interact with MongoDB. However, upon running it, I encounter the following error message: Starting... (node:7863) Unhandl ...

The Vue property or method is unrecognized when utilizing the non-minified version

When I attempted to display the name 'John' using an inline template in a simple Vue example, I encountered the following error message: [Vue warn]: Property or method "name" is not defined on the instance but referenced during render. ...

Trouble with transmitting jQuery form information to PHP page

Having mostly worked with PHP, I am now exploring new territories, so please bear with me if this sounds too simple. My goal is to set up a basic registration form that triggers a PHP function to store the entered data. However, it seems like the data is ...

Despite the presence of data within the array, the React array returns empty when examined using the inspect element tool

Currently, I'm working on creating an array of objects in React. These objects consist of a name and a corresponding color, which is used to represent the pointer on Google Maps. For instance, one object would look like this: {name: "Ben", colour: "gr ...

Remove leading and trailing spaces from all items in the list

I have a list called my_list my_list = [' Cat Alfred ', ' Dog Austin Power ', ' Very very very lazy frog '] In order to remove spaces at the beginning and end of each item, while keeping spaces between characters, I want my ...

What causes variables and functions to behave differently when it comes to hoisting?

I've recently been delving into some documentation and have noticed some inconsistencies in hoisting patterns within JavaScript. Consider the following examples: When it comes to functions, function abc(){ console.log("worked") } abc(); OUTPUT : ...

Navigating attributes originating from the key in JSON: A guide to effective management

I'm facing a situation in my application where I have a JSON object structured like this: var pages = { home: { title: "Home", description: "The home page", file: "home.html", url: "/home" }, blog: { ...