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 := "[email protected]"
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 : [email protected]
2015/08/11 14:43:28 Normalized email : [email protected]
change to another domain name, the result will be different.
2015/08/11 14:51:18 Before : [email protected]
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
+6.1k Golang : Extract sub-strings
+15.4k Golang : Get digits from integer before and after given position example
+28.7k Golang : Get first few and last few characters from string
+14k Golang : How to convert a number to words
+21.9k Golang : Repeat a character by multiple of x factor
+18.4k Golang : Display list of time zones with GMT
+12.4k Golang : Pass database connection to function called from another package and HTTP Handler
+12.7k Golang : Get terminal width and height example
+8.6k Golang : HTTP Routing with Goji example
+7.9k Golang : How To Use Panic and Recover
+13.8k Golang : Google Drive API upload and rename example
+5.9k Golang : Convert Chinese UTF8 characters to Pin Yin