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
+22k Golang : Join arrays or slices example
+6k Javascript : Get operating system and browser information
+13.5k Golang : Query string with space symbol %20 in between
+20.1k Golang : How to get own program name during runtime ?
+10.3k Golang : cannot assign type int to value (type uint8) in range error
+7.1k Golang : Get environment variable
+30.3k Golang : How to verify uploaded file is image or allowed file types
+7.6k Golang : get the current working directory of a running program
+13.4k Facebook PHP getUser() returns 0
+12.1k Golang : Get remaining text such as id or filename after last segment in URL path
+11k Golang : Create S3 bucket with official aws-sdk-go package
+14.1k Elastic Search : Mapping date format and sort by date