form表单常用方法(各种常用校验及计算)

23 篇文章 0 订阅
18 篇文章 4 订阅

form表单常用方法

手机号校验

export const checkPhone = (data) => {
    let value = true
    let code = /^1[3456789]\d{9}$/;
    if (!code.test(data)) {
        value = false;
        console.log('手机号格式错误')
    }
    return value
}

电子邮箱校验

export const checkEmail = (data) => {
    let value = true
    let code = /(^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z]{2,6}$)/;
    if (!code.test(data)) {
        value = false;
        console.log('电子邮箱格式错误')
    }
    return value
}

邮编校验

export const checkZipCode = (data) => {
    let value = true
    let code = /(^[0-9]{6}$)|(^$)/;
    if (!code.test(data)) {
        value = false;
        console.log('邮编格式错误')
    }
    return value
}

姓名校验

export const checkName = (data) => {
    let value = true
    let code = /^[\u4E00-\u9FA5]+(·[\u4E00-\u9FA5]+)*$/;
    if (!code.test(data)) {
        value = false;
        console.log('姓名格式错误')
    }
    return value
}

身份证校验,并提取年龄、生日、性别信息

注意:此方法只能粗略校验,不够细致

export const checkIdNum = (data) => {
	let info = {
		age: '',
		sex: '',
		sexName: '',
		brith: '',
		value: ''
	}
	let code =
		/(^[1-9]\d{5}[1-2]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}(\d|X|x)$)|(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)/;
	if (!code.test(data)) {
		info.value = false
		console.log('身份证号格式错误')
	} else {
		// 提取性别信息,0表示男性,1表示女性
		let sexs = data.substring(16, 17)
		if (sexs % 2 === 0) {
			info.sex = 1
			info.sexName = '女'
		} else {
			info.sex = 0
			info.sexName = '男'
		}
		// 提取生日信息
		info.brith = data.substring(6, 10) + '-' + data.substring(10, 12) + '-' + data.substring(12, 14);
		// 计算年龄
		let briths = new Date(info.brith)
		let now = new Date()
		info.age = now.getFullYear() -
			briths.getFullYear() -
			(now.getMonth() < briths.getMonth() ||
				(now.getMonth() == briths.getMonth() &&
					now.getDate() < briths.getDate()) ?
				1 :
				0);
		// 身份证号是否符合格式
		info.value = true;
	}
	return info
}

根据出生日期计算年龄

注意:需传入 yyyy-mm-dd | yyyy/mm/dd 格式的值

export const obtainAge = (data) => {
    let briths = new Date(data)
	let now = new Date()
	let age = now.getFullYear() -
		briths.getFullYear() -
		(now.getMonth() < briths.getMonth() ||
			(now.getMonth() == briths.getMonth() &&
				now.getDate() < briths.getDate()) ?
			1 :
			0);
	return age
}

数字添加单位(万、亿)

注意:传入的值需要能被parseFloat()方法转换

export const countUnit = (value) => {
	let count = parseFloat(value)
	let detail = {
		value: 0,
		unit: ''
	}
	if (count < 10000) {
		detail.value = count;
		detail.unit = ''
	} else if (10000 <= count < 100000000) {
		detail.value = (count / 10000).toFixed(2);
		detail.unit = '万'
	} else {
		detail.value = (count / 100000000).toFixed(2);
		detail.unit = '亿'
	}
	return detail
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

温其如玉_zxh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值