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
+14.7k Golang : Save(pipe) HTTP response into a file
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer
+20.8k Golang : Get password from console input without echo or masked
+10.1k Golang : Convert file content to Hex
+4.4k Adding Skype actions such as call and chat into web page examples
+6.5k Unix/Linux : How to fix CentOS yum duplicate glibc or device-mapper-libs dependency error?
+17.3k Golang : delete and modify XML file content
+3.4k Golang : Switch Redis database redis.NewClient
+11.7k Golang : Clean formatting/indenting or pretty print JSON result
+14.2k Golang : How to determine if user agent is a mobile device example
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+14.9k Golang : How to get Unix file descriptor for console and file