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
+50.5k Golang : How to get time in milliseconds?
+7.6k Golang : Convert(cast) []byte to io.Reader type
+37.8k Golang : How to iterate over a []string(array)
+5.9k Golang : Takes a plural word and makes it singular
+4.7k Golang : Grab news article text and use NLP to get each paragraph's sentences
+21.7k Golang : Gorilla mux routing example
+12.4k Golang : Activate web camera and broadcast out base64 encoded images
+9.1k Golang : Convert file content to Hex
+22.1k Golang : Fix type interface{} has no field or no methods and type assertions example
+18.5k Android Studio : AlertDialog and EditText to get user string input example
+9.9k Golang : Characters limiter example
+12.5k Golang : Check if an integer is negative or positive