2

this is my video

i put some spaces in front the @ mark but the submit button doesn't disappear, why? That is still a wrong mistake value and i want the submit button disapear.

my code is like this

<html>
<head>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-resource.min.js"></script>
</head>
<body ng-app="ctrljs">
    <form name='myForm' ng-controller='formctrl'>
        <input type='email' name='email' ng-model='email' required ng-pattern="/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/">
        <span ng-show='myForm.email.$error.email'> <font color='red'>invalid email</font></span>
        <input type='submit' value='submit' ng-hide="!myForm.$valid"></input>
    </form>
    <script>
        var app = angular.module('ctrljs', []);
        app.controller('formctrl', function($scope, $http){
          $scope.digits = {};
        });
    </script>
</body>
</html>

help me please, thank you.

Juna serbaserbi
  • 205
  • 2
  • 12

1 Answers1

3

The email is valid because angular will trim that whitespace for you. See the example below. If you type a space before example. You will see it become valid.

var app = angular.module('ctrljs', []);
app.controller('formctrl', function($scope, $http) {
  $scope.email = "   example@example.com";
});
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular-resource.min.js"></script>

<body ng-app="ctrljs">
  <form name='myForm' ng-controller='formctrl'>
    <pre>{{email}}</pre>
    <input type='email' name='email' ng-model='email' required ng-pattern="/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/">
    <span ng-show='myForm.email.$error.email'> <font color='red'>invalid email</font></span>
    <input type='submit' value='submit' ng-hide="!myForm.$valid">
  </form>
</body>

Unfortunately there is no ng-trim for type="email" see the docs here:

https://docs.angularjs.org/api/ng/input/input%5Bemail%5D

If you want to prevent the auto trimming from happening you have to monkey patch.


Also from the doc, the type=email input already has a built in validation using the regex in this file: https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js

You don't need to provide your own unless you need stricter or looser validation.

dting
  • 38,604
  • 10
  • 95
  • 114
  • Thank you @DTing for your explain answer, i had tried in my programme and the result is there's no space in front of the string, your answer is correct. thanks again. – Juna serbaserbi Aug 03 '15 at 07:39
  • DTing, please help me again in [here](http://stackoverflow.com/questions/33669459/confusing-about-this-cookies-in-redirecting-system) – Juna serbaserbi Nov 12 '15 at 12:43