如何在angularjs中使用正则表达式

如何在angularjs中使用正则表达式,angularjs,validation,Angularjs,Validation,我试过这个代码 <script> angular.module('emailExample', []) .controller('ExampleController', ['$scope', function($scope) { $scope.text = 'me@example.com'; }]); </script> <form name="myForm" ng-controller="ExampleController"&g

我试过这个代码

<script>
  angular.module('emailExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.text = 'me@example.com';
    }]);
</script>
  <form name="myForm" ng-controller="ExampleController">
    Email: <input type="email" name="input" ng-model="text" required>
    <span class="error" ng-show="myForm.input.$error.required">
      Required!</span>
    <span class="error" ng-show="myForm.input.$error.email">
      Not valid email!</span>
    <tt>text = {{text}}</tt><br/>
    <tt>myForm.input.$valid = {{myForm.input.$valid}}</tt><br/>
    <tt>myForm.input.$error = {{myForm.input.$error}}</tt><br/>
    <tt>myForm.$valid = {{myForm.$valid}}</tt><br/>
    <tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br/>
    <tt>myForm.$error.email = {{!!myForm.$error.email}}</tt><br/>
  </form>

angular.module('emailExample',[])
.controller('ExampleController',['$scope',function$scope){
$scope.text='1me@example.com';
}]);
电邮:
必修的!
无效电子邮件!
text={{text}}
myForm.input.$valid={{myForm.input.$valid}}
myForm.input.$error={{{myForm.input.$error}}
myForm.$valid={{myForm.$valid}}}
myForm.$error.required={{{!!myForm.$error.required}}}
myForm.$error.email={{{!!myForm.$error.email}}}

但在验证中遇到了问题。如果我试图进入a@a.c'在电子邮件中,则不会显示任何错误。请在这方面帮助我。

我在想你为什么不使用
ng模式来解决这个问题。您可以创建一个正则表达式并放入
ng模式。

例如:

<form name="signupFrm">

    <input type="email", ng-pattern="emailPattern">

</form>

根据angular email validation,这是正确的。使用ng模式使其更精确我已经尝试过,但没有使用函数Ctrl($scope){$scope.text='enter email';$scope.word=/^[a-z]+[a-z0-9.]+@[a-z]+\.[a-z.]{2,5}$/}无效的电子邮件!
    <div ng-class="{'has-error': signupFrm.email.$dirty && signupFrm.email.$invalid}">
        <input type="email" placeholder="Email" name="email" ng-pattern="emailPattern" required="required">
        <div ng-show="signupFrm.email.$dirty && signupFrm.email.$invalid || signupFrm.email.$error.pattern">
           <span ng-show="signupFrm.email.$error.pattern && signupFrm.email.$invalid " class="help-block"> should look like an email address</span>
           <span ng-show=" signupFrm.email.$error.required" class="help-block">can't be blank</span>
       </div> 
  </div>
$scope.emailPattern = /^([a-zA-Z0-9])+([a-zA-Z0-9._%+-])+@([a-zA-Z0-9_.-])+\.(([a-zA-Z]){2,6})$/;