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
+11.9k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+6.6k Swift : substringWithRange() function example
+10.9k Golang : Web routing/multiplex example
+10k Golang : Random Rune generator
+20.1k nginx: [emerg] unknown directive "passenger_enabled"
+22.6k Golang : simulate tail -f or read last line from log file example
+13.3k Golang : reCAPTCHA example
+8.6k Golang : Get final balance from bit coin address example
+27.2k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+9.9k Golang : Test a slice of integers for odd and even numbers
+9.5k Golang : interface - when and where to use examples