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
+6.9k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+12.1k Golang : Convert a rune to unicode style string \u
+6.5k Golang : Handling image beyond OpenCV video capture boundary
+19.3k Golang : Check whether a network interface is up on your machine
+20.3k Golang : Compare floating-point numbers
+6.9k Golang : Decode XML data from RSS feed
+10.1k Golang : Convert octal value to string to deal with leading zero problem
+19.9k Golang : Archive directory with tar and gzip
+33.9k Golang : convert(cast) bytes to string
+9.2k Golang : Gonum standard normal random numbers example
+48.7k Golang : Upload file from web browser to server
+12.9k Golang : Convert int(year) to time.Time type