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
+13.9k Golang : Reverse IP address for reverse DNS lookup example
+46.2k Golang : Encode image to base64 example
+5.8k AWS S3 : Prevent Hotlinking policy
+12.8k Golang : Get terminal width and height example
+6.4k Grep : How to grep for strings inside binary data
+13.9k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+8.4k Golang : Another camera capture GUI application with GTK and OpenCV
+8k Android Studio : Rating bar example
+15k Golang : Save(pipe) HTTP response into a file
+8.7k Golang : HTTP Routing with Goji example
+9.4k Golang : How to extract video or image files from html source code
+14.5k Golang : Execute function at intervals or after some delay