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
+10.7k SSL : The certificate is not trusted because no issuer chain was provided
+6.7k Golang : Rename part of filename
+7.4k Golang : Reverse text lines or flip line order example
+10.9k Golang : convert(cast) float to string
+5.1k Golang : Generate multiplication table from an integer example
+8.5k Random number generation with crypto/rand in Go
+21.5k Golang : Convert Unix timestamp to UTC timestamp
+8.7k Golang : Copy map(hash table) example
+4.9k Golang : Markov chains to predict probability of next state example
+34.8k Golang : Validate IP address
+16.1k Golang : How to tell if a file is compressed either gzip or zip ?
+9.2k Golang : Compare files modify date example