2

I tried this to disallow characters

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

but it's not working.

4
  • What characters do you want to disallow? @, #, &, and -? Then try "/^[^@#&-]*$/" Commented Aug 18, 2016 at 8:21
  • You have to add your ng-model to your input: ngPattern adds the pattern validator to ngModel Commented Aug 18, 2016 at 8:22
  • @Wiktor Stribiżew it works. Can you post it on answer so I can select it as the right/correct answer. Thanks! Commented Aug 18, 2016 at 8:25
  • @ralcazar: I posted the solution. Commented Aug 18, 2016 at 9:05

2 Answers 2

4

If you want to disallow @, #, &, and - characters, use "/^[^@#&-]*$/":

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

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

  • ^ - start of string
  • [^@#&-]* - 0 or more chars other than the ones defined in the negated character class
  • $ - end of string.

Note: to make the value required, you may add required attribute, or replace the * quantifier in the pattern with + (to require at least 1 (or more) characters).

Sign up to request clarification or add additional context in comments.

1 Comment

^@#&- means except these set of character. +1 for explaination
1

Here a working example of ngPattern excluding @, #, &, and - characters with the use of 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 is restricted by the current 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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.