正则验证总结(自用)
1.身份证号验证
仅限18位新版身份证号
var pattern =/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
2.邮箱验证
目前网上的邮箱验证都有点问题,部分错误情况也会判断正确
var pattern = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
3.邮编验证
6位数的验证
var pattern = /^[0-9]{6}$/;
4.电话号码验证
包括手机号和座机号,座机号由区号-座机号组成。
var pattern = /^((0\d{2,3}-\d{7,8})|(1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}))$/;
5.密码效验
8-20位带数字字母下划线的密码
var pattern = /^(\w){8,16}$/;
6.用户名效验
字母或数字组成的2-30位用户名
var pattern = /^[a-zA-Z0-9]{2,30}$/;
function isNullOrEmpty(value) {
if ((value == null || value == undefined || value == "null" || value == "undefined" || value.toString() == "")) {
return true;
} else {
return false;
}
}
//下拉框输入值验证
function onComboValidation(e) {
var items = this.findItems(e.value);
if (!items || items.length == 0) {
e.errorText = "输入值不在下拉数据中";
e.isValid = false;
}
}
//身份证号验证
function onIDCardValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
//var pattern = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
var pattern =/^([1-6][1-9]|50)\d{4}(18|19|20)\d{2}((0[1-9])|10|11|12)(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的身份证号";
e.isValid = false;
}
}
}
}
//邮箱
function onEmailValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/;
//var pattern =/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的邮箱";
e.isValid = false;
}
}
}
}
//验证邮编6位
function onZipCodeValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^[0-9]{6}$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的邮编";
e.isValid = false;
}
}
}
}
//验证电话号码
function onPhoneNumValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
//var pattern = /^((0\d{2,3}-\d{7,8})|(\d{7,8})|(1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}))$/;
var pattern = /^((0\d{2,3}-\d{7,8})|(1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}))$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的电话格式(手机号或固定电话)\n手机:11位手机号码 \n固话:区号-座机号(例如:0930-7898888)";
e.isValid = false;
}
}
}
}
//验证固定电话电话号码
function onFixedPhoneAreaCodeValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^((0\d{2,3}-\d{7,8}(-\d{3,})?)|(\d{7,8}(-\d{3,})?))$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的固话格式:区号-座机号(例如:0930-7898888)";
e.isValid = false;
}
}
}
}
//验证固定电话电话号码-带分机
function onFixedPhoneExtensionValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^((0\d{2,3}-\d{7,8}(-\d{3,})?)|(\d{7,8}(-\d{3,})?))$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的固话格式:例如0931-76458961-626(分机是可选项)";
e.isValid = false;
}
}
}
}
//验证区号
function onAreaCodeValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^0\d{2,3}$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的区号,例如:0930";
e.isValid = false;
}
}
}
}
//验证固话
function onFixedPhoneValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^(\d{7,8})$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的固定电话,例如:76458961";
e.isValid = false;
}
}
}
}
//验证手机号11位
function onMobilePhoneValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\d{8}$/;
if (pattern.test(val) == false) {
e.errorText = "请输入正确的11位手机号";
e.isValid = false;
}
}
}
}
//密码校验
function onPasswordValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^(\w){8,16}$/;
if (pattern.test(e.value) == false) {
e.errorText = "请输入6-20位字母、数字或下划线";
e.isValid = false;
}
}
}
}
//用户名校验
function onUsernameValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^[a-zA-Z0-9]{2,30}$/;
if (pattern.test(e.value) == false) {
e.errorText = "请输入2-30位字母或数字";
e.isValid = false;
}
}
}
}
//验证社会统一信用代码
function onSocialCodeValidation(e) {
var val=e.value.replace(/\s+/g,"");
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
var pattern = /^(11|12|13|19|51|52|53|59|91|92|93|Y1)\d{6}\w{9}\w$/;
var pattern1 = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (pattern.test(val) || pattern1.test(val)) {
e.isValid = true;
}else{
e.requiredErrorText = "请输入正确的社会统一信用代码或身份证号,如:12620100720200270R";
e.isValid = false;
}
}
}
}
//非零
function onNeqZeroValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
if (isNaN(e.value) || Number(e.value) === 0) {
e.errorText = "请输入非0的数字";
e.isValid = false;
}
}
}
}
//大于0
function onGtZeroValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
if (isNaN(e.value) || Number(e.value) <= 0) {
e.errorText = "请输入大于0的数字";
e.isValid = false;
}
}
}
}
//大于等于0
function onGteZeroValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
if (isNaN(e.value) || Number(e.value) < 0) {
e.errorText = "请输入大于等于0的数字";
e.isValid = false;
}
}
}
}
//小于0
function onLtZeroValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
if (isNaN(e.value) || Number(e.value) >= 0) {
e.errorText = "请输入小于0的数字";
e.isValid = false;
}
}
}
}
//小于等于0
function onLteZeroValidation(e) {
if (!isNullOrEmpty(e.value)) {
if (e.isValid) {
if (isNaN(e.value) || Number(e.value) > 0) {
e.errorText = "请输入小于等于0的数字";
e.isValid = false;
}
}
}
}