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
+9.6k Golang : Translate language with language package example
+9.2k Golang : Quadratic example
+6.6k Golang : Takes a plural word and makes it singular
+2.9k Golang : Fix go-cron set time not working issue
+20k Golang : Read directory content with os.Open
+9.2k Golang : Copy map(hash table) example
+10.1k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+9.3k Golang : Eroding and dilating image with OpenCV example
+23.6k Golang : Find biggest/largest number in array
+4.5k Which content-type(MIME type) to use for JSON data
+9.8k Golang : Random Rune generator