2

I know ng-pattern can be used to allow characters. I'm looking for a way to just limit one specific character.

My input box should work with any characters except the verticle bar '|' . I want to either throw an error just not allow it to be typed out. Is there an easy way to just not allow that specific character. Or should I just use ng-pattern to include EVERYTHING except the vertical bar? ( how would I do that?)

My html:

                     <td class="col-xs-3">
                        <input type="text"
                              name="Application"
                              ng-model="list.Application"
                              maxlength="64"
                              required />

                           <div ng-messages="addForm.Application.$error"
                                 ng-if="addForm.$submitted"

                        </div>
                    </td>

enter image description here

mdewitt
  • 2,526
  • 19
  • 23
Angular
  • 603
  • 2
  • 9
  • 24

1 Answers1

1

You need to add an ng-pattern attribute to the input and use a regex that will restrict textual input.

This regex must match the text, thus, you need to check if the full input has no pipe symbols.

You need:

  • ^ - start of string
  • [^|]* - zero or more symbols other than | (replace * with + to disallow empty input)
  • $ - end of string.

Thus, use

ng-pattern="/^[^|]*$/"

The /.../ are regex delimiters that will let angular know we are passing a regex object rather than a string.

Also, ng-pattern="[^|]*" should also work since string patterns are anchored by default in Angular.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563