字母和连字符的ng模式

时间:2019-04-03 11:41:16

标签: angularjs regex

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

我尝试了上述操作,但不接受连字符。我该怎么写?

示例输入:“不带引号的“约瑟夫-傅里叶”

1 个答案:

答案 0 :(得分:1)

我正在共享示例代码,请检查:

<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