Skip to content

Instantly share code, notes, and snippets.

@ferdiunal
Created November 26, 2016 22:45
Show Gist options
  • Save ferdiunal/7ecce04cce4a9b34d30e8ed0071dd2b4 to your computer and use it in GitHub Desktop.
Save ferdiunal/7ecce04cce4a9b34d30e8ed0071dd2b4 to your computer and use it in GitHub Desktop.
Tester
package main
import (
"net"
"regexp"
"strconv"
"strings"
)
var IBANS = map[string]interface{}{
"AD": "AD[0-9]{2}[0-9]{4}[0-9]{4}[A-Z0-9]{12}",
"AE": "AE[0-9]{2}[0-9]{3}[0-9]{16}",
"AL": "AL[0-9]{2}[0-9]{8}[A-Z0-9]{16}",
"AO": "AO[0-9]{2}[0-9]{21}",
"AT": "AT[0-9]{2}[0-9]{5}[0-9]{11}",
"AZ": "AZ[0-9]{2}[A-Z]{4}[A-Z0-9]{20}",
"BA": "BA[0-9]{2}[0-9]{3}[0-9]{3}[0-9]{8}[0-9]{2}",
"BE": "BE[0-9]{2}[0-9]{3}[0-9]{7}[0-9]{2}",
"BF": "BF[0-9]{2}[0-9]{23}",
"BG": "BG[0-9]{2}[A-Z]{4}[0-9]{4}[0-9]{2}[A-Z0-9]{8}",
"BH": "BH[0-9]{2}[A-Z]{4}[A-Z0-9]{14}",
"BI": "BI[0-9]{2}[0-9]{12}",
"BJ": "BJ[0-9]{2}[A-Z]{1}[0-9]{23}",
"BR": "BR[0-9]{2}[0-9]{8}[0-9]{5}[0-9]{10}[A-Z][A-Z0-9]",
"CH": "CH[0-9]{2}[0-9]{5}[A-Z0-9]{12}",
"CI": "CI[0-9]{2}[A-Z]{1}[0-9]{23}",
"CM": "CM[0-9]{2}[0-9]{23}",
"CR": "CR[0-9]{2}[0-9]{3}[0-9]{14}",
"CV": "CV[0-9]{2}[0-9]{21}",
"CY": "CY[0-9]{2}[0-9]{3}[0-9]{5}[A-Z0-9]{16}",
"CZ": "CZ[0-9]{2}[0-9]{20}",
"DE": "DE[0-9]{2}[0-9]{8}[0-9]{10}",
"DK": "DK[0-9]{2}[0-9]{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{2}[0-9]{4}[0-9]{6}[0-9]{10}",
"SM": "SM[0-9]{2}[A-Z]{1}[0-9]{5}[0-9]{5}[A-Z0-9]{12}",
"SN": "SN[0-9]{2}[A-Z]{1}[0-9]{23}",
"TL": "TL38[0-9]{3}[0-9]{14}[0-9]{2}",
"TN": "TN59[0-9]{2}[0-9]{3}[0-9]{13}[0-9]{2}",
"TR": "TR[0-9]{2}[0-9]{5}[A-Z0-9]{1}[A-Z0-9]{16}",
"VG": "VG[0-9]{2}[A-Z]{4}[0-9]{16}",
"XK": "XK[0-9]{2}[0-9]{4}[0-9]{10}[0-9]{2}",
}
var (
latitude = regexp.MustCompile("^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?)$")
longitude = regexp.MustCompile("^[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$")
ssn = regexp.MustCompile(`^\d{3}[- ]?\d{2}[- ]?\d{4}$`)
tcNo = regexp.MustCompile("^[1-9]{1}[0-9]{9}[0,2,4,6,8]{1}$")
mongo = regexp.MustCompile("^[0-9a-fA-F]{24}$")
alpha = regexp.MustCompile("^[a-zA-Z]+$")
alphaNum = regexp.MustCompile("^[a-zA-Z0-9]+$")
numberic = regexp.MustCompile("^[-+]?[0-9]+$")
upper = regexp.MustCompile("^[A-Z]+$")
lower = regexp.MustCompile("^[a-z]+$")
imei = regexp.MustCompile(`^\d{15}(,\d{15})*$`)
base64 = regexp.MustCompile("^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$")
visa_card = regexp.MustCompile("^4[0-9]{6,}$")
american_express = regexp.MustCompile("^3[47][0-9]{5,}$")
master_card = regexp.MustCompile("^5[1-5][0-9]{5,}|222[1-9][0-9]{3,}|22[3-9][0-9]{4,}|2[3-6][0-9]{5,}|27[01][0-9]{4,}|2720[0-9]{3,}$")
)
func main() {
}
// .IP("127.0.0.1") // TRUE OR FALSE
func IP(str string) bool {
ip := net.ParseIP(str)
return ip != nil && ip.To4() != nil || ip != nil && ip.To16() != nil
}
// .PORT("8080") // TRUE OR FALSE
func PORT(str string) bool {
if i, e := strconv.Atoi(str); e == nil && i > 0 && i < 65536 {
return true
}
return false
}
// string ip address check IP version 4
func IPv4(str string) bool {
ip := net.ParseIP(str)
return ip != nil && ip.To4() != nil
}
// string ip address check IP version 6
func IPv6(str string) bool {
ip := net.ParseIP(str)
return ip != nil && ip.To16() != nil
}
// string mac address check
func MAC(str string) bool {
_, err := net.ParseMAC(str)
return err == nil
}
// Checked Coordinate Latitude
func Latitude(str string) bool {
return latitude.MatchString(str)
}
// Checked Coordinate Longitude
func Longitude(str string) bool {
return longitude.MatchString(str)
}
// Checked Turkish Republic ID Number
func TCNO(str string) bool {
return tcNo.MatchString(str)
}
// Checked Social Security Number
func SSN(str string) bool {
return ssn.MatchString(str)
}
func toFloat64(str string) (float64, error) {
res, err := strconv.ParseFloat(str, 64)
if err != nil {
res = 0.0
}
return res, err
}
func toInt(str string) (int64, error) {
res, err := strconv.ParseInt(str, 0, 64)
if err != nil {
res = 0
}
return res, err
}
func isAlpha(is string) bool {
return alpha.MatchString(is)
}
func isAlphaNum(is string) bool {
return alphaNum.MatchString(is)
}
func isAlphaUpper(is string) bool {
return upper.MatchString(is)
}
func isAlphaLower(is string) bool {
return lower.MatchString(is)
}
func isNumeric(is string) bool {
return numberic.MatchString(is)
}
func isBetween(str string, first int, second int) bool {
between := len(str)
return between >= first || between <= second
}
func MongoID(str string) bool {
return mongo.MatchString(str)
}
func isIban(country string, iban string) bool {
match := regexp.MustCompile(strings.ToUpper(country))
return match.MatchString(iban)
}
func Imei(str string) bool {
return imei.MatchString(str)
}
func Base64(str string) bool {
return base64.MatchString(str)
}
func isAmericanExpress(str string) bool {
return american_express.MatchString(str)
}
func isMasterCard(str string) bool {
return master_card.MatchString(str)
}
func isVisaCard(str string) bool {
return visa_card.MatchString(str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment