Skip to content

Instantly share code, notes, and snippets.

@erasin
Created August 26, 2013 09:39
Show Gist options
  • Save erasin/6339708 to your computer and use it in GitHub Desktop.
Save erasin/6339708 to your computer and use it in GitHub Desktop.
字段验证实例 by build web application with golang
package verify
import (
"regexp"
)
// 验证年龄是为数字
func Is_age(s string) bool {
if m, _ := regexp.MatchString("^[0-9]+$", s); !m {
return false
}
return true
}
// 验证中文
func Is_ch(s string) bool {
if m, _ := regexp.MatchString("^[\\x{4e00}-\\x{9fa5}]+$", s); !m {
return false
}
return true
}
// 验证英文
func Is_en(s string) bool {
if m, _ := regexp.MatchString("^[a-zA-Z]+$", s); !m {
return false
}
return true
}
// 验证email
func Is_email(s string) bool {
if m, _ := regexp.MatchString(`^([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})$`, s); !m {
return false
} else {
return true
}
}
// 验证手机号码
func Is_phone(s string) bool {
if m, _ := regexp.MatchString(`^(1[3|4|5|8][0-9]\d{4,8})$`, s); !m {
return false
}
return true
}
// 验证身份证号码
func Is_usercard(s string) bool {
//验证15位身份证,15位的是全部数字
if m, _ := regexp.MatchString(`^(\d{15})$`, s); !m {
return false
}
//验证18位身份证,18位前17位为数字,最后一位是校验位,可能为数字或字符X。
if m, _ := regexp.MatchString(`^(\d{17})([0-9]|X)$`, s); !m {
return false
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment