0

I'm using the native HTML5 validation for an "email" field and it works fine! However, I would like to increase it to a specific case, where I do not want to accept emails with "free" domains (gmail, hotmail, etc).

I did the regular expression and tested it and it worked correctly (Here you can do the test: https://regex101.com/r/wBt3YN/1). But when applying to the pattern of the email field, nothing happens.

How to proceed?

Some strings:

[email protected] -> Can't allow

[email protected] -> Can allow

[email protected] -> Can't allow

Regex Pattern

^([\w-.]+@(?!gmail\.com)(?!yahoo\.com)(?!hotmail\.com)(?!mail\.com)(?!live\.com)(?!aol\.com)(?!outlook\.com)(?!bol\.com)(?!msn\.com)(?!ymail\.com)([\w-]+.)+[\w-]{2,4})?$ 

My form

<form> <div class="field"> <label for="email">Email Corporativo</label> <input type="email" name="email" id="email" value="" pattern="^([\w-.]+@(?!gmail\.com)(?!yahoo\.com)(?!hotmail\.com)(?!mail\.com)(?!live\.com)(?!aol\.com)(?!outlook\.com)(?!bol\.com)(?!msn\.com)(?!ymail\.com)([\w-]+.)+[\w-]{2,4})?$" title="Utilize seu email corporativo" placeholder="" required > </div> <input type="submit" value="ENVIAR"> </form> 
7
  • Did you test your assertions about how the form is being processed using web client dev tools? Commented Sep 13, 2018 at 14:42
  • @jdv how can i do that? Commented Sep 13, 2018 at 14:46
  • Most browsers have a "developer" mode. For example, Chrome on Windows uses F12. Your browser/platform may be different, but simply searching "browser developer mode" will get you all the info you need. Commented Sep 13, 2018 at 14:54
  • i got it on my chrome, but i dont know how can it help me. Commented Sep 13, 2018 at 15:04
  • Where can i see some useful information about that on my console? Commented Sep 13, 2018 at 15:04

1 Answer 1

2

Here is my code where I do not allow yahoo & hotmail. However, e-mail validation is a very delicate thing.

<form> <div class="field"> <label for="email">Email Corporativo</label> <input type="email" name="email" id="email" value="" pattern="^[^@]+@(?!(yahoo|hotmail))[^@]+\.[a-z]{2,}$" title="Utilize seu email corporativo" placeholder="" required > </div> <input type="submit" value="ENVIAR"> </form>

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.