vue+elementUI 表单校验抽离方法 validate.js
1、新建validate.js 文件
2、所需页面引用
3、使用方式入图
/**
* @param {string} path
* @returns {Boolean}
*/
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
/**
* @param {string} str
* @returns {Boolean}
*/
export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}
// 手机号
export const isMobile = (s) => {
return /^1[0-9]{10}$/.test(s)
}
// 值验证身份证长度或格式错误
export const isIDCard = (idCard) => {
return /(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(idCard)
}