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
+5.9k Linux/MacOSX : Search for files by filename and extension with find command
+18.3k Golang : Logging with logrus
+5.6k Swift : Get substring with rangeOfString() function example
+28.3k Golang : Change a file last modified date and time
+21.5k SSL : How to check if current certificate is sha1 or sha2
+7.9k Findstr command the Grep equivalent for Windows
+8.9k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+24.9k Golang : Create PDF file from HTML file
+8k Golang : Append and add item in slice
+11.6k Golang : How to detect a server/machine network interface capabilities?
+8.7k Golang : Gaussian blur on image and camera video feed examples
+15.7k Golang : Update database with GORM example