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
+33.5k Golang : Upload and download file to/from AWS S3
+13.4k Golang : package is not in GOROOT during compilation
+17.7k Golang : Delete duplicate items from a slice/array
+15k Golang :Trim white spaces from a string
+5.4k Golang : Convert an executable file into []byte example
+8.6k Golang : Turn string or text file into slice example
+10.6k Golang : convert(cast) float to string
+21.5k Golang : Get ASCII code from a key press(cross-platform) example
+28k Golang : How to declare kilobyte, megabyte, gigabyte, terabyte and so on?
+11.6k Golang : Transform comma separated string to slice example
+18.2k Golang : Delete item from slice based on index/key position
+10.4k Swift : Convert (cast) Float to String