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
+2.6k Golang : Display advertisement images or strings on random order
+7.4k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+2.6k Golang : Rot13 and Rot5 algorithms example
+5k Golang : Sort and reverse sort a slice of floats
+5.6k Golang : Set image canvas or background to transparent
+3.3k Golang : Get environment variable
+1.9k Javascript : Access JSON data example
+3.4k Golang : Validate credit card example
+3.7k Golang : Format strings to SEO friendly URL example
+3.9k Golang : Regular Expression find string example
+2.5k Golang : How to get capacity of a slice or array?
+3.3k Android Studio : Image button and button example