Golang : Normalize email to prevent multiple signups example
If your application allows the public to sign up, chances are your application will attract the "unkind" types and they will attempt to sign up multiple times with the same email address by mixing symbols, upper and lower cases and adding dots to make variants of the same email address.
To prevent this sort of multiple sign ups, you can try to normalize(canonicalize - treat them as the same) the email addresses by removing the unwanted cases, symbols,etc. Below is an example of how to normalize email address with govalidator.NormalizeEmail()
function.
At this moment, the NormalizeEmail()
function only supports gmail.com accounts. You might want to see the source code in govalidator
package to modify it for your own requirement.
package main
import (
"github.com/asaskevich/govalidator"
"log"
)
func main() {
email := "sammy+SPammer@gmail.com"
normalized, err := govalidator.NormalizeEmail(email)
if err != nil {
log.Fatal(err)
}
log.Println("Before : ", email)
log.Println("Normalized email : ", normalized)
}
Output :
NOTE : IF gmail, anything after + symbol will be removed
2015/08/11 14:43:28 Before : sammy+SPammer@gmail.com
2015/08/11 14:43:28 Normalized email : sammy@gmail.com
change to another domain name, the result will be different.
2015/08/11 14:51:18 Before : sammy+SPammer@somewhere.com
2015/08/11 14:51:18 Normalized email : sammy
+spammer
@somewhere.com
Reference :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+13.2k Golang : Read XML elements data with xml.CharData example
+26k Golang : Convert(cast) string to uint8 type and back to string
+29.9k Golang : How to verify uploaded file is image or allowed file types
+20.9k Golang : Convert(cast) string to rune and back to string example
+5.8k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+7.8k Golang : Get all countries phone codes
+29.2k Golang : Login(Authenticate) with Facebook example
+5k Golang : Calculate half life decay example
+10k Golang : Embed secret text string into binary(executable) file
+32.8k Delete a directory in Go
+6k Golang & Javascript : How to save cropped image to file on server
+17.9k Golang : How to remove certain lines from a file