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
+4.9k Golang : Launching your executable inside a console under Linux
+35.1k Golang : Convert date or time stamp from string to time.Time type
+5.8k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+7.8k Golang : Capture text return from exec function example
+4.7k Golang : Grab news article text and use NLP to get each paragraph's sentences
+6.3k Golang : Getting Echo framework StartAutoTLS to work
+8.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+7.8k Golang : Simple histogram example
+18.6k Golang : Reset or rewind io.Reader or io.Writer
+18k Golang : How to get own program name during runtime ?
+29.8k Golang : Convert an image file to []byte
+18.8k Golang : Read directory content with os.Open