Logo

dev-resources.site

for different kinds of informations.

โ›”Stop Excessive Validation, you are ruining my life hack!

Published at
11/20/2024
Categories
validation
lifehack
productivity
webdev
Author
diso
Author
4 person written this
diso
open
โ›”Stop Excessive Validation, you are ruining my life hack!

How many websites do we all have to register with, too many, and if they require an email address then you can often get frustrated and have to find ways around using your regular email address on hundreds of sites.

I've always used Plus Addressing, if you don't know it, it is fairly simple.

Rather than sign up for a new streaming service with [email protected], I use [email protected]. The email will still be delivered to the same email address of [email protected], but the 'streaming' bit I added to the email address will allow me to see who the email is from, if they have passed my email address on to anyone else I will know which site it was, and I can easily sort (well, auto delete), emails with 'streaming' in the address using a rule in my email client.

โŒ Failing Validation

It has become more common recently that when using this trick the form I am using the email address on doesn't allow it, and I fail a validation check.

I got annoyed about this, had a moan, and moved on. ๐Ÿคฌ

That is until doing a recent code review and finding this. ๐Ÿซจ

^[a-zA-Z0-9%]+@[a-zA-Z0-9-]+\.[a-zA-Z]{2,3}$

A bit of Regex to validate the email address. I'm sure the Regex experts among you have already seen one or two issues, I'll get to those in a moment.

I was pretty sure the person who'd written this didn't know much, if anything, about Regex, they had certainly never used it before. Plus, there was a comment above it in the code which didn't look like a real code comment, but rather more like a prompt for AI ๐Ÿ’ป to generate some code.

Indeed, I checked out quite a lot of different AIs, asking them for some Regex to validate an email address, and the responses were mixed, some did well, and some did very badly and would reject a lot of valid email addresses.

โ“ The Problems

So what are the problems? Well, in the first part of the address, we are not allowing the + symbol, so my life hack is blown. In fact, we also don't allow other symbols, so [email protected] won't be able to sign up.

In the domain section, sub-domains wouldn't be allowed. So if you worked for a company that had multiple sites, you could have an email address something like [email protected] or [email protected], and wouldn't be able to sign up.

The top-level domain is also limited, to 2-3 characters, as someone who has websites with top-level domains of .cooking and .menu for a couple of restaurants, I found this very annoying.

Amazingly, I had to explain why this was bad, I guess a lot of people are just used to .com and .org and not much else. One person thought that the address part of the email could only include letters, numbers and a full stop (period for our USA friends), they had no idea you could use a + or a - in an email address.

๐Ÿ“งโ˜Ž๏ธ๐Ÿ”  It Isn't Just Emails

As I continued with the code review I discovered it wasn't just emails, someone had learned what Regex was and by golly, they were going to use it.

Phone numbers, that must only contain numbers! No no, in different countries people format phone numbers in different ways, some use brackets, some use hyphens, and many use spaces.

Full name, validating that they have used one space (and only one space) and only characters. Hang on, have you heard of double-barrelled names? What if someone wants to put in a first name, middle name and last name, that wouldn't be allowed. What would Spiderman's ๐Ÿ•ท๏ธ๐Ÿ•ธ๏ธ girlfriend Mary Jane do? By the way, you only allowed Latin alphabet characters too.

๐Ÿ’ก The Solution

Some of the issues with this particular Regex might be obvious, but the point remains, did it really need that level of validation anyway?

The solution is simple, don't over-validate things.

Sure, check if an email has an @ symbol in it, but beyond that, allow it. If someone wants to format their phone numbers in a way that is strange to you, let them.

We have to trust the end user a little bit, don't we?

validation Article's
30 articles in total
Favicon
"yup" is the new extra virgin olive oil
Favicon
JavaScript Email Validation Regex: Ensuring Accuracy in User Inputs
Favicon
Simplifying JSON Validation with Ajv (Another JSON Validator)
Favicon
Dynamic string validation using go's text/template package
Favicon
Practical Email Validation Using JavaScript: Techniques for Web Developers
Favicon
Validation in Spring REST Framework (SRF)
Favicon
Terraform Validation Rules: Best Practices & Examples
Favicon
Zod for TypeScript Schema Validation: A Comprehensive Guide
Favicon
AI Image Validation Solutions: A Game-Changer for Quality Control
Favicon
Transform zod error into readable error response
Favicon
โ›”Stop Excessive Validation, you are ruining my life hack!
Favicon
zod vs class-validator & class-transformer
Favicon
Animated Login Form with Validation Using HTML, CSS & JavaScript
Favicon
How to work with regular expressions
Favicon
User Form Validation in HTML CSS and JavaScript | JavaScript Form Validation
Favicon
Simplify Form Validation with FormGuardJS: A Lightweight and Flexible Solution
Favicon
moment.js Alternative - Luxon
Favicon
Validating REST requests in a NestJS application and displaying errors in Angular application forms
Favicon
Implement data validation in FastAPI
Favicon
The Definitive Guide to the Constraint Validation API
Favicon
Env-Core: An Easy Way to Validate Environment Variables in Node.js Projects
Favicon
File-Type Validation in Multer is NOT SAFE๐Ÿ™ƒ
Favicon
Guaranteed GxP Compliance: Mastering Validation Testing Strategies
Favicon
Assuring Excellence: The Crucial Benefits of GxP Validation Testing
Favicon
7 Old-School Practices in HTML Should Be Avoided
Favicon
Implement data validation in Spring Boot
Favicon
Implement data validation in Express
Favicon
An easy way to validate DTO's using Symfony attributes
Favicon
Implement data validation in .NET
Favicon
Mastering Data Validation in NestJS: A Complete Guide with Class-Validator and Class-Transformer

Featured ones: