Golang : Validate credit card example
Problem :
You have an e-commerce website and you want to validate the user credit card number before passing it to the payment gateway or your own credit card processor. How to validate credit card number?
Solution :
Use the govalidator.IsCreditCard()
function to validate the user's credit card.
For example :
package main
import (
"fmt"
"github.com/asaskevich/govalidator"
)
func main() {
// from http://www.freeformatter.com/credit-card-number-generator-validator.html
ccNumber := "5176865765334720"
validCreditCard := govalidator.IsCreditCard(ccNumber)
fmt.Printf("%s is a valid credit card : %v \n", ccNumber, validCreditCard)
}
Output :
5176865765334720 is a valid credit card : true
NOTE : This is just to ensure that the number is valid, but you will have to do more security checks - such as CCV number, expiry dates, etc
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
+7.2k Golang : Get environment variable
+7.5k Golang : Handling Yes No Quit query input
+12k Golang : Determine if time variables have same calendar day
+13.2k Golang : Handle or parse date string with Z suffix(RFC3339) example
+18.4k Golang : Read binary file into memory
+6.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+21.8k SSL : How to check if current certificate is sha1 or sha2
+7.3k Golang : Of hash table and hash map
+20.7k Golang : Secure(TLS) connection between server and client
+4.8k MariaDB/MySQL : Form select statement or search query with Chinese characters
+13.5k Golang : Get constant name from value
+14.3k Golang : Get uploaded file name or access uploaded files