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
+19.3k Golang : Check whether a network interface is up on your machine
+7.1k Golang : Takes a plural word and makes it singular
+6.4k Golang : Selection sort example
+28.6k Golang : Change a file last modified date and time
+7k Fix sudo yum hang problem with no output or error messages
+8.8k Golang : How to join strings?
+8k Golang : Trim everything onward after a word
+7.2k Golang : Transform lisp or spinal case to Pascal case example
+8.3k Golang : Routes multiplexer routing example with regular expression control
+6k Facebook : How to force facebook to scrape latest URL link data?
+14.6k Golang : Overwrite previous output with count down timer
+5.4k Golang : Pad file extension automagically