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
+12.1k Golang : error parsing regexp: invalid or unsupported Perl syntax
+10.2k Golang : Post data with url.Values{}
+22.5k Golang : Generate MD5 checksum of a file
+11.5k Golang : How to calculate the distance between two coordinates using Haversine formula
+16.7k Golang : Get command line arguments
+17.5k Golang : Get host name or domain name from IP address
+21.1k Generate checksum for a file in Go
+13.3k Golang : Convert(cast) int to float example
+17.4k Unmarshal/Load CSV record into struct in Go
+7.3k Golang : Convert word to its plural form example
+7.6k Golang : Gaussian blur on image and camera video feed examples
+4.5k Clean up Visual Studio For Mac installation failed disk full problem