Thunfische
Thunfische

Reputation: 1157

ng-pattern for letters and hyphens

<input name="name" type="text" ng-maxlength="100" placeholder="name" required ng-pattern="/^[a-zA-Z\s\-]*$/">

I tried the above but it did not accept hyphens. How can I write it?

example input: "joseph-fourier" without quotes

Upvotes: 0

Views: 754

Answers (1)

Ajinkya
Ajinkya

Reputation: 345

I am sharing a sample code Please check :

<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<html>
    <script src="https://code.angularjs.org/1.6.9/angular.js"></script>
<script>
    angular.module('AppExample', [])
      .controller('ExampleController', ['$scope', function($scope) {
        $scope.regex = '^[a-zA-Z\-]*$';  // Regex for String with hyphens
      }]);
  </script>
  <body ng-app="AppExample">
  <div ng-controller="ExampleController">
    <form name="form">
      
      <label for="input">Enter name (hyphens allowed)</label>
      <input type="text" ng-model="model" id="input" name="input" ng-pattern="{{regex}}" />
      Is input valid = <code>{{form.input.$valid}}</code><br>
  </div>
</body>
  </html>

Valid String Output

Upvotes: 1

Related Questions