-1

Want to remove below characters from user input

!@#$%^&*()-+=\|[]{}/:;',>.<?

For example user enter below value

"Test1 Dat!@#$%^&*()-+=\|[]{}/:;',>.<?a Re_enter

Expected result = Test1 Data Re_enter

I have tried regular expression but i did't get exact one

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
   
    $scope.submit = function() {
        var data = $scope.user;
        console.log(data);
    };
    
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="formCtrl">
  <form novalidate>
    Data:<br>
    <input type="text" ng-model="user.data"><br>
  
    <br><br>
    <button ng-click="submit()">SUBMIT</button>
  </form>
  
</div>
martin kumar
  • 65
  • 1
  • 2
  • 9

2 Answers2

1

For user expirience it would be better if you didnt allow entering those characters at first time. Try to use onChange event of <input type="text" ng-model="user.data"> and filter new value with regex, only after that set filtered value to model.

You can find appropriate regexp at this answer

layonez
  • 1,746
  • 1
  • 16
  • 20
1

The Input Element has a property called pattern

There are RegExp which allow you to restrict your input from certain patterns like yours

I think it would be enough, just to allow Word-Characters (/\w*/g)

<input type="text" pattern='*Your RegExp*' ng-model="user.data"><br>

That was the HTML way

Alternatively you can check the sent input Data in the React-Based FrontEnd or at the deep end in the BackEnd. There are many solutions :)

mgreif
  • 131
  • 6