2

Is there any pattern that will allow only '-' in the input text box and deny entering other special characters like @, #, $ etc.I tried with the pattern ng-pattern="/^[a-zA-Z0-9]*$/" which will deny all special characters from being entered in the field.

   <form name="myForm">
    <input type="text" ng-pattern="/^[a-zA-Z0-9]*$/" ng-model="name" name="name">
    </form>
  <span class="error pop_up"  ng-show="myForm.name.$error.pattern">Special Characters are not allowed</span>
forgottofly
  • 2,729
  • 11
  • 51
  • 93

1 Answers1

5

Put the hyphen at the end of your character class. When it is the last character it is assumed as a literal hyphen. For example: /^[a-zA-Z0-9-]*$/

<form name="myForm">
   <input type="text" ng-pattern="/^[a-zA-Z0-9-]*$/" ng-model="name" name="name">
</form>
<span class="error pop_up"  ng-show="myForm.name.$error.pattern">Special Characters are not allowed</span>
Donal
  • 31,121
  • 10
  • 63
  • 72