domain after @ - HTML

2

Can anyone tell me how I can input only one email with one domain and reject another?

Ex:

[email protected] - ok
[email protected] - erro

HTML

<input type="text" name="nome" class="txt_input first_input" placeholder="Nome" required>
                        <input type="email" name="email" class="txt_input" id="email" placeholder="E-mail" required>
                        <input type="email" name="confirmaEmail" class="txt_input" id="confirma-email" placeholder="Confirmar e-mail" required>
    
asked by anonymous 08.08.2016 / 16:10

2 answers

4

If it is only with HTML , you can use the Pattern Attribute and validate with Regex, as in the example below:

<form name="myForm">
  <input type="email" name="email" pattern="[A-Za-z0-9._%+-][email protected]" required="required" />
  <button type="submit">Enviar</button>
</form>
  

As reminded by the @GustavoTinoco , pattern does not support Safari in the Desktop version. The full list of supported browsers (Desktop and Mobile) can be viewed here.

    
08.08.2016 / 16:35
3

You can also do the validation using the following regular expression in Javascript :

var regex = /@gmail\.com$/;

var gmail = regex.test('[email protected]');

var hotmail = regex.test('[email protected]');

document.writeln('[email protected]: ' + gmail);

document.writeln('<br/>');

document.writeln('[email protected]: ' + hotmail);

The expression @gmail\.com is in charge of capturing the existence of @gmail.com , and $ forces the expression to be found at the end.

    
08.08.2016 / 16:46