Encyclopedia of front-end js regular verification (a complete set of regular verification solutions) @莫成尘

Look at the code first, just copy and use it, and the verification is based on the regular verification that is commonly used in the front-end. (Personal used to name the hump, please forgive me)

If you are satisfied, please give Mo Chengchen a Fabulous

Mobile phone number verification

思路是以1开头的第二位是23456789,匹配数字字符9位并结束共计11位

function PhoneNum(value) {
    
    
	return /^1[23456789]\d{
    
    9}$/.test(value);
}

Fixed phone number verification

思路是以1开头的第二位是23456789,匹配数字字符9位并结束共计11位

function FixedPhoneNum(value) {
    
    
	return /^\d{
    
    3,4}-\d{
    
    7,8}(-\d{
    
    3,4})?$/.test(value);
}

E-mail verification

思路是匹配字母、数字、下划线开头中间有@符号匹配字母、数字、下划线结尾

function Email(value) {
    
    
	return /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(value);
}

ID verification

思路是匹配数字字符开头18位数并允许最后一位是X

function IDCard(value) {
    
    
    let reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
	return reg.test(value)
}

Time and date verification

思路是不是非法的或者NaN类型的可被new Date方法转换 警告 ios下时间转换需要将 - 转换为 /

function NewDate(value) {
    
    
	return !/Invalid|NaN/.test(new Date(value).toString());
}

IOS time and date verification

警告 ios下时间转换需要将 - 转换为 /

function NewDateOfIos(value) {
    
    
	return /^\d{
    
    4}[\/\-](0?[1-9]|1[012])[\/\-](0?[1-9]|[12][0-9]|3[01])$/.test(value);
}

Whether it is a decimal number verification

思路是摒弃:-。,.等是否存在 ,不存在的数字字符验证即是十进制数字

function NumberIsTen(value) {
    
    
	return /^(?:-?\d+|-?\d{
    
    1,3}(?:,\d{
    
    3})+)?(?:\.\d+)?$/.test(value);
}

Digital integer verification

思路是匹配一个数字字符。等价于 [0-9]。开头并以他结束

function WhNumber(value) {
    
    
	return /^\d+$/.test(value);
}

Amount verification

思路是为数字并且小数点后保留一位两位(金额,只允许保留两位小数)

function Money(value) {
    
    
	return /^[1-9]\d*(,\d{
    
    3})*(\.\d{
    
    1,2})?$|^0\.\d{
    
    1,2}$/.test(value);
}

Chinese Verification

思路是根据国际utf-8编码,匹配中文

function IsChinese(value) {
    
    
	const reg = /^[\u4e00-\u9fa5]+$/gi;
	return reg.test(value);
}

English verification

思路是'a' 到 'z' 范围内的任意小写字母字符开头并结束的。

function IsEnglish(value) {
    
    
	return /^[a-zA-Z]*$/.test(value);
}

Does A contain B verification

思路是通过indexOf方法查找不为-1即存在

function IsContains(A, B) {
    
    
	return A.indexOf(B) >= 0;
}

SMS verification code verification

思路是4位或者6位的数字

function IsCode(value, length = 6 || length= 4) {
    
    
	return new RegExp(`^\\d{
     
     ${
      
      length}}$`).test(value);
}

Object verification

您肯定见过类似的对象输出,转化为字符串然后判断是否全等,从底层方法判断,避免兼容或者容错

function IsObject(value) {
    
    
	return Object.prototype.toString.call(value) === '[object Object]';
}

Whether array validation

您也可以使用isArray或者别的js方法

function IsArr(value) {
    
    
	return Object.prototype.toString.call(value) == "[object Array]";
}

Whether it is the license plate number verification (ordinary vehicle)

方法借鉴与uview 框架Link: uview-UniappU frame URL

function IsCarOfOld(value) {
    
    
	const creg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳]{1}$/;
	if (value.length === 7) {
    
    
		return creg.test(value);
	} else {
    
    
		return false;
	}
}

Whether it is the license plate number verification (new energy vehicles)

方法借鉴与uview 框架Link: uview-UniappU frame URL

function IsCarOfNew(value) {
    
    
	const xreg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}(([0-9]{5}[DF]$)|([DF][A-HJ-NP-Z0-9][0-9]{4}$))/;
	if (value.length === 8) {
    
    
		return xreg.test(value);
	} else {
    
    
		return false;
	}
}

Call method

Such as:

	let A = 15398031234
	console.log(this.PhoneNum(A))

The above is the common regular verification summarized by individuals based on their professional experience (partially borrowed from the uview framework, thanks to the selfless open source of the uview team).
Other questions about js or areas that you don't understand this method can leave a message, and I will reply and help you solve it as soon as possible.

Guess you like

Origin blog.csdn.net/weixin_47821281/article/details/108801864