Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular validate input type="email" as type="text"

My Angular app has a login form with an email/username input field:

<input type="email" required/>

Input is type="email" so the correct keyboard is displayed in iOS.
However validation should be that of a type="text" input to allow for a username.

Basically I need override Angular to validate type="email" as type="text"
Here is a basic Plunker to get started.

Note: I've tried using ng-pattern to validate on regex but it only seems to work for type="text"

like image 610
Oisin Lavery Avatar asked Jul 30 '13 17:07

Oisin Lavery


People also ask

How do you validate email input?

When you create an email input with the proper type value, email , you get automatic validation that the entered text is at least in the correct form to potentially be a legitimate e-mail address. This can help avoid cases in which the user mistypes their address, or provides an invalid address.

Which input type is used to validate an 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.

How do I validate an email in template driven form?

We can also validate an email using pattern attribute in template-driven forms. To use PatternValidator, we'll add pattern attribute to the form input controls. It'll check whether the value matches with the supplied regex pattern or not and based on that it'll update the validation status of the input.

Is email validation in angular 4 can be achieved using email validator?

2 Methods of email validation in Angular Built-in validator: Angular features several built-in email validators, including EmailValidator. You can use them to verify the user-provided email addresses. Pattern validation: Pattern validation enables you to specify a regular expression (regex).


1 Answers

I don't think it's possible to change how input type='email' is handled in angular without changing it's source, but maybe instead you could try to:

<input type="text" required late-email />
angular.module('yourApp').directive('lateEmail', function () {
    return {
        priority: -1,
        link: function(scope, elem, attrs) {
            elem.attr('type', 'email');
        }
    };
});

Important thing here is priority, as angularjs input directive needs to run(link) first so will use 'text' as input type, then lateEmail will change input type to email.

like image 184
Tadeusz Wójcik Avatar answered Sep 21 '22 18:09

Tadeusz Wójcik