email-validator.dart
email-validator.dart copied to clipboard
Return reason for invalidity
Hey 👋 Thanks for your library!
I wanted to check if you would consider including a way, to provide the reason the validation fails? Maybe by returning enum values or something.
I guess many use the library to validate an email input, but then can only add an error message like "Please enter a valid email address", while most users don't really get why the entered address isn't working. Especially if they added an accidental whitespace at the end.
We are currently adding some additional validations for certain cases like whitespaces, but this also means we start to duplicate logic from your library, which doesn't feel right.
Looking forward to hear your thoughts on this. Cheers
I'm not sure whether that`'s useful information, you could always just strip the whitespaces before/after for them @woolfred?
Whitespace is an example where some users got stuck and therefore came to my head first. This is something we can workaround with trimming for sure.
But maybe they miss the @
sign and continue. At the end it will just say: "Invalid email", while I guess the validation will fail at a specific step, checking for the @
-sign, which could be communicated.
In the end I just wanted to check, if this is something you considered or would consider. As I guess this might have some challenges and/or might bloat the library.
I could look into something, sounds like an interesting feature for sure. Could you elaborate on your app/use case/product?
Sure. We have a flutter app, which has to deal with user accounts, so we have forms for sign-up, forgot password or updating the e-mail address, which all have an email input.
We have some wrapper logic, to deal with validation on blur and then run through a list of provided validators like:
(email) => EmailValidator.validate(email ?? '')
, to show an error messages.
After some time we got complains of users, which weren't able to register as it always said: "Invalid e-mail address" and the button stayed disabled. Luckily we could see on a screenshot of a user, that there might been an whitespace upfront.
So the quickest fix was adding a validator checking on whitespaces before EmailValidator.validate
, to give more specific feedback of why it failed. (Trimming or removing whitespace, lead to some problems with the form state, due to our wrapper. It is solvable, but considering the effort, we went for the validator).
So this is where the idea was coming from, that when we have an external validation library, it would be nice to get specific feedback about the reason. Also looking at most examples how to do field validation out there, it looks like this:
validator: (String? value) {
return (value != null && value.contains('@')) ? 'Do not use the @ char.' : null;
},
So I guess that is the place in flutter apps where most people use EmailValidator
and could improve the feedback.
In the meantime we added to our TextField
s this line:
inputFormatters: [DenyWhitespace()],
Which is:
class DenyWhitespace extends FilteringTextInputFormatter {
DenyWhitespace() : super.deny(RegExp(r'\s'));
}
Which just ignores whitespace you enter into a field. Also when copy & pasting it strips any whitespace from the string.
This is why I came up with the other example of typos like forgetting an @
sign, which user should catch, when the field tells you that it is invalid, as it is more obvious than a whitespace. But if the validator could actually say "it is invalid because of a missing @-sign", it could improve the user experience even more.
Does this help?