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.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+6.6k Golang : How to solve "too many .rsrc sections" error?
+24.1k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+10.6k Nginx : TLS 1.2 support
+7.7k Golang : Handle Palindrome string with case sensitivity and unicode
+17.4k Golang : Iterate linked list example
+6.5k Golang : Get expvar(export variables) to work with multiplexer
+5.7k nginx : force all pages to be SSL
+6.9k Golang : Transform lisp or spinal case to Pascal case example
+7.6k Golang : Get today's weekday name and calculate target day distance example
+18.9k Mac OSX : Homebrew and Golang
+14.2k Golang : Reset buffer example