Hris

Hris

I have some troubles with E-mail validations inside of the form:

<%= text_input f, :email, placeholder: "e-mail", class: "form-control", required: true, pattern: (\w+)@([\w.]{2,3})\.([\w.]{2,3}) %>

The browser recognizes the pattern but it does not work properly. It always pops an error.
I tried many different e-mail validations and at the end I get the same result.

First 10 of 12 Posts Switch mode

cnck1387

cnck1387

You could use email_input instead of text_input and it will validate emails for you. Another nice perk of using that field is it optimizes character layouts on mobile devices to make it easier to enter an email address.

But, don’t forget to validate your email address on the server side with a changeset. Never trust information from the client directly. Someone could easily change the HTML from email_input to text_input or remove your pattern, etc..

LostKobrakai

LostKobrakai

Also keep in mind that accurately validating email addresses is still best be done by confirming it via an actual email sent to the address. Regexes so short as yours will never match all the crazy addresses, which are actually valid based on the spec: Email address - Wikipedia

Hris

Hris OP

It works that way.. Kinda.. It stops when I write , for example, “abv@abc” . It does not count the patern: "..." . The validation dissapears at the first letter after @.

kannix

kannix

Why do you use that pattern? Do you want to restrict the field to some special mail addresses only?

I would suggest to use email_input without an additional pattern. There is no good regex to test for an email address (read more over here: Stop Validating Email Addresses with Regex by @davidcelis)

kannix

kannix

And a small tip for working with regex expressions. Use an online tool like https://regex101.com to check and understand the regex term.

By this you will pretty fast find a few of the errors with that regex:

(\w+)@([\w.]{2,3})\.([\w.]{2,3})

  • (\w+) does not include . so something like hris.lastname in front of the @ will not work
  • ([\w.]{2,3}) matches only 2 or 3 characters so @gmail is not valid for this regex
  • \.([\w.]{2,3}) something like a .ninja toplevel domain will not match
Hris

Hris OP

In my case, pattern does not work, although my browser sees it. I tried different regex combinations. I want to restrict the field for specific emails like @gmail.com. I have changeset validation but I also need it to be in the form.

kannix

kannix

Can you give more detail on this? Do you get any validation message at all? Do you see an error? Does it submit the form without checking the regex?

What browser are you using? What are the exact steps you are trying?

I just tried an minimal example at https://jsfiddle.net/uq65mwgh/4/

The html i used is this:

<form action="">
  <input type="email" pattern="a.*">
  <input type="submit">
</form>

This works for me in the following way:

  • if i click submit with an empty input field I get the email validation error
  • if i enter something like foo@bar.de and click on submit I get the validation error from the regex (the regex simply checks for an email address starting with the character a
  • if i enter afoo@bar.de the browser submits the form
pedromvieira

pedromvieira

We have a Global SaaS platform and email is really important to us.
After several issues with non RFC compliant systems (like Sendgrid), we implemented a combination of Trim, Downcase, Regex, Email Checker and Unidecode.

Regex - Regex.match?(~r/^[A-Za-z0-9\._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,6}$/, email)
Email Checker - email_checker | Hex
Unidecode - unidecode | Hex

dimitarvp

dimitarvp

Awesome job!

One remark though:

That’s rare but, some SMTP define a catchall email address. Meaning all emails using this domain seems valid even if they are not.

I plan to set my email exactly like that. And then I will have emails like hackernews@mydomain.com or linkedin@mydomain.com etc. so I can keep track of which organization got my email from where.

Your package wants to deem such addresses invalid? I understand some spam orgs use techniques like these but many people use them with valid reasons.

toraritte

toraritte

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement