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.5k Javascript : Shuffle or randomize array example
+38.4k Golang : Read a text file and replace certain words
+16k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+15.4k Golang : package is not in GOROOT during compilation
+30.1k Golang : Record voice(audio) from microphone to .WAV file
+52.9k Golang : How to get struct field and value by name
+8.1k Javascript : Put image into Chrome browser's console
+5.4k Python : Create Whois client or function example
+22k SSL : How to check if current certificate is sha1 or sha2
+5.7k PHP : Convert CSV to JSON with YQL example
+6.9k Golang : Find the longest line of text example
+18.3k Golang : Check if a directory exist or not