复制
、是否合法IP 地址
export function validateIP (rule, value,callback ) {
if (value=='' ||value==undefined ||value==null ){
callback ();
}else {
const reg = /^(\d{,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/ ;
if ((!reg.test (value)) && value != '' ) {
callback (new Error ('请输入正确的IP地址' ));
} else {
callback ();
}
}
}、是否手机号码或者固话
export function validatePhoneTwo (rule, value, callback ) {
const reg = /^((\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/ ;;
if (value == '' || value == undefined || value == null ) {
callback ();
} else {
if ((!reg.test (value)) && value != '' ) {
callback (new Error ('请输入正确的电话号码或者固话号码' ));
} else {
callback ();
}
}
}、是否固话
export function validateTelphone (rule, value,callback ) {
const reg =/\d{2,3}-\d{7,8}/ ;
if (value=='' ||value==undefined ||value==null ){
callback ();
}else {
if ((!reg.test (value)) && value != '' ) {
callback (new Error ('请输入正确的固定电话)' ));
} else {
callback ();
}
}
}、是否手机号码
export function validatePhone (rule, value,callback ) {
const reg =/^[][3-9][0-9]{9}$/ ;
if (value=='' ||value==undefined ||value==null ){
callback ();
}else {
if ((!reg.test (value)) && value != '' ) {
callback (new Error ('请输入正确的电话号码' ));
} else {
callback ();
}
}
}、是否身份证号码
export function validateIdNo (rule, value,callback ) {
const reg = /(^\d{}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ ;
if (value=='' ||value==undefined ||value==null ){
callback ();
}else {
if ((!reg.test (value)) && value != '' ) {
callback (new Error ('请输入正确的身份证号码' ));
} else {
callback ();
}
}
}、是否邮箱
export function validateEMail (rule, value,callback ) {
const reg =/^([a-zA-Z-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/ ;
if (value=='' ||value==undefined ||value==null ){
callback ();
}else {
if (!reg.test (value)){
callback (new Error ('请输入正确的邮箱' ));
} else {
callback ();
}
}
}、合法url
export function validateURL (url ) {
const urlregex = /^(https?|ftp):\/\/([a-zA-Z-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ ;
return urlregex.test (url);
}、验证内容是否包含英文数字以及下划线
export function isPassword (rule, value, callback ) {
const reg =/^[_a-zA-Z-9]+$/ ;
if (value=='' ||value==undefined ||value==null ){
callback ();
} else {
if (!reg.test (value)){
callback (new Error ('仅由英文字母,数字以及下划线组成' ));
} else {
callback ();
}
}
}、自动检验数值的范围
export function checkMax (rule, value, callback ) {
if (value == '' || value == undefined || value == null ) {
callback ();
} else if (!Number (value)) {
callback (new Error ('请输入[,20000]之间的数字' ));
} else if (value < || value > 20000 ) {
callback (new Error ('请输入[,20000]之间的数字' ));
} else {
callback ();
}
}、验证数字输入框最大数值
export function checkMaxVal (rule, value,callback ) {
if (value < || value > 最大值) {
callback (new Error ('请输入[,最大值]之间的数字' ));
} else {
callback ();
}
}、验证是否1 -99 之间
export function isOneToNinetyNine (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入正整数' ));
} else {
const re = /^[-9][0-9]{0,1}$/ ;
const rsCheck = re.test (value);
if (!rsCheck) {
callback (new Error ('请输入正整数,值为【,99】' ));
} else {
callback ();
}
}
},);
}、验证是否整数
export function isInteger (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入正整数' ));
} else {
const re = /^[-9]*[1-9][0-9]*$/ ;
const rsCheck = re.test (value);
if (!rsCheck) {
callback (new Error ('请输入正整数' ));
} else {
callback ();
}
}
},);
}、验证是否整数,非必填
export function isIntegerNotMust (rule, value, callback ) {
if (!value) {
callback ();
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入正整数' ));
} else {
const re = /^[-9]*[1-9][0-9]*$/ ;
const rsCheck = re.test (value);
if (!rsCheck) {
callback (new Error ('请输入正整数' ));
} else {
callback ();
}
}
},);
}、 验证是否是[0 -1 ]的小数
export function isDecimal (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入[,1]之间的数字' ));
} else {
if (value < || value > 1 ) {
callback (new Error ('请输入[,1]之间的数字' ));
} else {
callback ();
}
}
},);
}、 验证是否是[1 -10 ]的小数,即不可以等于0
export function isBtnOneToTen (rule, value, callback ) {
if (typeof value == 'undefined' ) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入正整数,值为[,10]' ));
} else {
if (!(value == '' || value == '2' || value == '3' || value == '4' || value == '5' || value == '6' || value == '7' || value == '8' || value == '9' || value == '10' )) {
callback (new Error ('请输入正整数,值为[,10]' ));
} else {
callback ();
}
}
},);
}、验证是否是[1 -100 ]的小数,即不可以等于0
export function isBtnOneToHundred (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入整数,值为[,100]' ));
} else {
if (value < || value > 100 ) {
callback (new Error ('请输入整数,值为[,100]' ));
} else {
callback ();
}
}
},);
}、验证是否是[0 -100 ]的小数
export function isBtnZeroToHundred (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (!Number (value)) {
callback (new Error ('请输入[,100]之间的数字' ));
} else {
if (value < || value > 100 ) {
callback (new Error ('请输入[,100]之间的数字' ));
} else {
callback ();
}
}
},);
}、验证端口是否在[0 ,65535 ]之间
export function isPort (rule, value, callback ) {
if (!value) {
return callback (new Error ('输入不可以为空' ));
}
setTimeout (() => {
if (value == '' || typeof (value) == undefined ) {
callback (new Error ('请输入端口值' ));
} else {
const re = /^([-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/ ;
const rsCheck = re.test (value);
if (!rsCheck) {
callback (new Error ('请输入在[-65535]之间的端口值' ));
} else {
callback ();
}
}
},);
}、验证端口是否在[0 ,65535 ]之间,非必填,isMust表示是否必填
export function isCheckPort (rule, value, callback ) {
if (!value) {
callback ();
}
setTimeout (() => {
if (value == '' || typeof (value) == undefined ) {
} else {
const re = /^([-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/ ;
const rsCheck = re.test (value);
if (!rsCheck) {
callback (new Error ('请输入在[-65535]之间的端口值' ));
} else {
callback ();
}
}
},);
}、小写字母
export function validateLowerCase (val ) {
const reg = /^[a-z]+$/ ;
return reg.test (val);
}、两位小数验证
const validateValidity = (rule, value, callback ) => {
if (!/(^[-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/ .test (value)) {
callback (new Error ('最多两位小数!!!' ));
} else {
callback ();
}
};、是否大写字母
export function validateUpperCase (val ) {
const reg = /^[A-Z]+$/ ;
return reg.test (val);
}、是否大小写字母
export function validatAlphabets (val ) {
const reg = /^[A-Za-z]+$/ ;
return reg.test (val);
}、密码校验
export const validatePsdReg = (rule, value, callback ) => {
if (!value) {
return callback (new Error ('请输入密码' ))
}
if (!/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\ue00-\u9fa5\s]){6,20}$/ .test (value)) {
callback (new Error ('请输入-20位英文字母、数字或者符号(除空格),且字母、数字和标点符号至少包含两种' ))
} else {
callback ()
}
}、中文校验
export const validateContacts = (rule, value, callback ) => {
if (!value) {
return callback (new Error ('请输入中文' ))
}
if (!/^[\u-\uFFE5A-Za-z]+$/ .test (value)) {
callback (new Error ('不可输入特殊字符' ))
} else {
callback ()
}
}、身份证校验
export const ID = (rule, value, callback ) => {
if (!value) {
return callback (new Error ('身份证不能为空' ))
}
if (! /(^\d{}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/ .test (value)) {
callback (new Error ('请输入正确的二代身份证号码' ))
} else {
callback ()
}
}、账号校验
export const validateCode = (rule, value, callback ) => {
if (!value) {
return callback (new Error ('请输入账号' ))
}
if (!/^(?![-9]*$)(?![a-zA-Z]*$)[a-zA-Z0-9]{6,20}$/ .test (value)) {
callback (new Error ('账号必须为-20位字母和数字组合' ))
} else {
callback ()
}
}、纯数字校验
export const validateNumber = (rule, value, callback ) => {
let numberReg = /^\d+$|^\d+[.]?\d+$/
if (value !== '' ) {
if (!numberReg.test (value)) {
callback (new Error ('请输入数字' ))
} else {
callback ()
}
} else {
callback (new Error ('请输入值' ))
}
}、最多一位小数
const onePoint = (rule, value, callback ) => {
if (!/^[-9]+([.]{1}[0-9]{1})?$/ .test (value)) {
callback (new Error ('最多一位小数!!!' ));
} else {
callback ();
}
};