What steps can I take to block certain characters using an AngularJS Regular Expression?

My attempt to prevent certain characters did not succeed

<input type="text" id="full_name" name="full_name" ng-pattern="/[^\@\-#\-&]/">

Unfortunately, the code is still allowing those characters.

Answer №1

To prevent the use of special characters like @, #, &, and -, you can utilize the regular expression "/^[^@#&-]*$/":

<input type="text" id="full_name" name="full_name" ng-model="YOUR_MODEL" ng-pattern="/^[^@#&-]*$/" />

The ^[^@#&-]*$ regex pattern explanation:

  • ^ - signifies the start of the string
  • [^@#&-]* - allows for zero or more characters excluding those specified within the negated character class
  • $ - marks the end of the string.

Note: You have the option to make the value mandatory by including the required attribute, or by replacing the * quantifier in the pattern with + (ensuring at least 1 or more characters are required).

Answer №2

Check out this practical demonstration of ngPattern which excludes @, #, &, and - characters using a Regular Expression: /^[^@#&-]+$/:

angular
  .module('App', [])
  .controller('ExampleController', function($scope) {
      $scope.full_name = '';
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="App" ng-controller="ExampleController">
  <form name="form">
    <label for="full_name">This input adheres to the specified pattern: </label>
    <input type="text" id="full_name" name="full_name" ng-model="full_name" ng-pattern="/^[^@#&-]+$/" /><br>
    <hr>
    Input valid? = <code>{{form.full_name.$valid}}</code><br>
    Model value = <code>{{full_name}}</code>
  </form>
</div>

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 is the process for importing a node module in React-Kotlin?

After creating my app using the create-react-kotlin-app command, it loaded successfully in Chrome. I then installed the React Material UI package via NPM without any issues. However, I am now facing the challenge of incorporating the Material UI module int ...

unable to retrieve correct value upon click

Within my setup, there exists a slider with accompanying left and right buttons. Two event handlers are present for each button as demonstrated in the code below. Upon clicking and allowing the transition to complete its process, everything functions corr ...

Utilizing Angular 5: Enhancing ngFor with a Pipe and a Click Event

Iterating through an array of objects using *ngFor, I apply various filters via pipes to manipulate the resulting list. One of these pipes relies on a user input from a search field. Upon clicking on one of the ngFor elements, the corresponding object is p ...

What is the best way to create a moving line using EaselJS and TweenJS?

My objective is to animate a line from point A to point B using the Tween function. I am utilizing the EaselJS drawing library and TweenJS for animation. Can I achieve this by using the moveTo function to animate a straight line from point A to point B? ...

I encountered difficulty accessing a different domain from the node server

I am currently in the process of integrating the PayUmoney payment gateway into my MEAN stack application. I have successfully retrieved all mandatory fields from the Angular controller to Node and even generated the Hash key. However, when attempting to r ...

Submitting a POST request using a Chrome Extension

I am in the process of developing a Chrome extension popup for logging into my server. The popup contains a simple form with fields for username, password, and a submit button. <form> <div class="form-group"> <label for="exampleInputE ...

retrieving the value of an object key based on changing information

console.log(x, obj.fares) //return undefined output adultFare Object {adultFare: "9.00", childFare: null, seniorCitizenFare: null, disabledFare: null,} How do I retrieve the adultFare value from the object? Is looping through the keys necessary? I expec ...

Exploring creative methods for incorporating images in checkboxes with CSS or potentially JavaScript

Although it may seem like a basic question, I have never encountered this particular task before. My designer is requesting something similar to this design for checkboxes (positioned on the left side with grey for checked boxes and white for unchecked). ...

Error: Unable to access the 'length' property of an undefined value | Solving the challenge of finding the shortest word within a string | Codewars Problem

I'm currently working on a function to find the shortest word in a given string of words. Issue: I encountered the error below TypeError: Cannot read property 'length' of undefined at findShort at Test.describe._ at /runner/fra ...

Ignore linting errors in the Webpack React Fast Refresh plugin for faster performance

I have integrated the following plugin for Hot Module Replacement (HMR): https://www.npmjs.com/package/@pmmmwh/react-refresh-webpack-plugin. How do I prevent it from blocking the page display due to non-breaking errors, such as linting issues? Here is a ...

Transferring a JavaScript array over to PHP

I am dealing with a situation where I have a list stored in a PHP file like this: <ul id="alist"> <li>Item1</li> <li>Item2</li> <li>Item3</li> </ul> By using jQuery, I have successfully captured that ...

Tips for efficiently waiting for the outcome in a unified function for multiple callbacks within node.js

Perhaps the question title is not the most appropriate, but let me explain what I am trying to achieve in my code // First Callback Function connection_db.query(get_measure_query,function(err,user_data1){ if(err){ // throw err; ...

What is the best way to handle an AJAX JSON response in AngularJS?

Need help with extracting properties from a JSON object returned by a REST service in AngularJS? Want to parse the firstName, lastName, and other attributes from the JSON response? Check out the code snippets below for guidance. Here is an example of an A ...

What could be causing the Express server to return an error despite everything being in order?

I am new to exploring express.js and attempting to create a basic sign-in example server. I have set up a database object with a users array containing objects, each with email and password properties. Despite using body-parser to convert the body into a j ...

Exploring various properties in React

I'm attempting to render a child component by simultaneously mapping multiple props. My goal is: const Parent = props => { const result = props.(***need to pass both props***).map((firstProp, secondProp) => ( <Child key={//} ...

Ways to display the data within a BLOB object

On this page, the user is showcasing a table with three columns - tipo_esame (string), data_esame (string), and uri (BLOB). const archiveItems = this.state.archive.map((archive, i) => { return ( <tr key={archive.hash_referral}> <td ...

Exploring Javascript/JQuery parameters based on data types

I'm a bit confused about whether the title accurately reflects my question. I need help understanding how jQuery deals with functions that are called with different argument combinations, such as ("Some String", true, function(){}) and ("Some String", ...

Creating new rows in PHP form using different ID and name pairs

I am seeking a solution to dynamically add rows of inputs using a button. I have come across several examples, but most of them change the name attribute of the HTML elements (e.g. name = 'price1', name = 'price2'), causing issues with ...

Conceal form after submission - Django 1.6

I'm currently working on a Django 1.6 project where I have this form: <form action="/proyecto/" method="POST" id="myform"> {% csrf_token %} <table> <span class="Separador_Modulo">& ...

Preventing autoscrolling in Ionic's dual side menu template when additional content is added

Could anyone kindly assist me in figuring out why the autoscrolling of the content is not functioning correctly? Whenever the button on the header is clicked, a new message will be included in the main content. However, once the number of lines exceeds wha ...