Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an ng-model and input type email bug?

Tags:

angularjs

Is there something special going on with input type="email" and ng-model attribute? If the input is email, then the model doesnt update. If I change the input type to text, number or date it updates correctly.

Bug or some special magic email validation behavior that I dont understand?

like image 698
Roger Johansson Avatar asked Jul 04 '13 10:07

Roger Johansson


People also ask

What validation is done for the input type email?

The <input type="email"> defines a field for an e-mail address. The input value is automatically validated to ensure it is a properly formatted e-mail address. To define an e-mail field that allows multiple e-mail addresses, add the "multiple" attribute.

What is@ ngModel?

ngModel is a directive which binds input, select and textarea, and stores the required user value in a variable and we can use that variable whenever we require that value. It also is used during validations in a form. We can use ngModel with: input.

How does ngModel work?

NgModel works using these two bindings together. First, it passes the data from the component class and set data in FormControl. Second, it passes the data from UI after a specific action is triggered and then changes the value of the control.

What is difference between ng model and Ng bind?

ngModel usually use for input tags for bind a variable that we can change variable from controller and html page but ngBind use for display a variable in html page and we can change variable just from controller and html just show variable.


4 Answers

It does some validation on then input, so you need to have entered a valid email address before it is bound to the model.

This is the regex that is used:

/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/

Basically you need to input an address that is at least [email protected]

like image 122
Derek Ekins Avatar answered Oct 25 '22 16:10

Derek Ekins


It's not a bug, it's only update when we type correct email address format for email validation. Add this attribute ng-model-options="{'allowInvalid': true}" to allow invalid email input.

like image 32
Mohammed Aslam Avatar answered Oct 25 '22 14:10

Mohammed Aslam


A complement, you can use properties on you form to see if your email is valid, like this :

HTML:

<form name="myForm" ng-submit="submit()">
    <input type="email" ng-model="email1" name="email1" />
</form>

Javascript:

//[formName].[inputFieldName].property 
myForm.email1.$pristine;
// Boolean. True if the user has not yet modified the form.
myForm.email1.$dirty
// Boolean. True if the user has already modified the form.
myForm.email1.$valid
// Boolean.True if the the form passes the validation.
myForm.email1.$invalid
// Boolean. True if the the form doesn't pass the validation.
myForm.email1.$error

Reference

like image 23
EpokK Avatar answered Oct 25 '22 15:10

EpokK


Starting from Angular 1.3, you can easily overwrite the 'email' validator and make it always return true.

angular
  .module('myApp', [])
  .controller('MainController', function() {
    this.email = '';
  })
  .directive('noEmailValidation', function() {
    return {
      restrict: 'A',
      require: 'ngModel',
      link: function(scope, elm, attr, ctrl) {
        ctrl.$validators['email'] = function() {
          return true;
        };
      }
    }
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<div ng-app="myApp">
  <form ng-controller="MainController as main">
    <div>Email: {{main.email}}</div>
    <input type="email" ng-model="main.email" no-email-validation>
  </form>
</div>

Enjoy.

like image 41
AdirAmsalem Avatar answered Oct 25 '22 16:10

AdirAmsalem