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
+28k Golang : missing Git command
+5.9k Golang : Levenshtein distance example
+35k Golang : Display float in 2 decimal points and rounding up or down
+5.1k Golang : Debug with Godebug
+11.4k Golang : HTTP response JSON encoded data
+5.2k Golang : Build new URL for named or registered route with Gorilla webtoolkit example
+20.1k Golang : Convert(cast) string to rune and back to string example
+15k Golang : How to extract links from web page ?
+7.1k Prevent Write failed: Broken pipe problem during ssh session with screen command
+7.5k Golang : How to capture return values from goroutines?
+8.4k Golang : Quadratic example
+8.8k Golang : Check if user agent is a robot or crawler example