0%

常用的表单验证

1、判断字符串是否是 https?:|mailto:|tal: 开头的
1
2
3
export function isExternal(path) {
return /^(https?:|mailto:|tel:)/.test(path)
}
2、检验字符串是否是 admin editor
1
2
3
4
export function validUsername(str) {
const valid_map = ['admin', 'editor']
return valid_map.indexOf(str.trim()) >= 0
}
3、判断网址
1
2
3
4
export function validURL(url) {
const reg = /^(http?|https?|ftp):\/\/([a-zA-Z0-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 reg.test(url)
}
4、判断都是小写字母
1
2
3
4
export function validLowerCase(str) {
const reg = /^[a-z]+$/
return reg.test(str)
}
5、判断都是大写字母
1
2
3
4
export function validUpperCase(str) {
const reg = /^[A-Z]+$/
return reg.test(str)
}
6、判断是只能是数字和字母
1
2
3
4
export function validAlphabets(str) {
const reg = /^[0-9A-Za-z]+$/
return reg.test(str)
}
7、用户密码格式
1
2
3
export function isEvolPass(str) {
return /^(?=.*[0-9])(?=.*[a-zA-Z]).{8,30}$/.test(str) // 用户密码格式正则
}
8、邮箱校验
1
2
3
4
export function isEmail(email) {
const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
return reg.test(email)
}
9、验证字符串
1
2
3
4
5
6
export function isString(str) {
if (typeof str === 'string' || str instanceof String) {
return true
}
return false
}
10、手机号码校验
1
2
3
export function isPhone(str) {
return /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[0-9])\d{8}$/.test(str) // 手机号码正则
}
11、验证是数组
1
2
3
4
5
6
export function isArray(arg) {
if (typeof Array.isArray === 'undefined') {
return Object.prototype.toString.call(arg) === '[object Array]'
}
return Array.isArray(arg)
}
12、判断只能是汉字
1
2
3
4
export function isChinese(str){
const reg=/^[\u0391-\uFFE5]+$/;
return reg.test(str)
}
13、校验时间大小
1
2
3
4
5
6
7
8
9
10
export function checkDate(obj){
const obj_value=obj.replace(/-/g,"/");//替换字符,变成标准格式(检验格式为:'2009-12-10')
// var obj_value=obj.replace("-","/");//替换字符,变成标准格式(检验格式为:'2010-12-10 11:12')
const date1=new Date(Date.parse(obj_value));
const date2=new Date();//取今天的日期
if(date1>date2){
return false; //不能大于当前时间
}
return true
}
14、判断ip
1
2
3
4
5
6
7
8
export function isIP(strIP) {
if (isNull(strIP)) return false;
const re=/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/g //匹配IP地址的正则表达式
if(re.test(strIP)){
if( RegExp.$1 <256 && RegExp.$2<256 && RegExp.$3<256 && RegExp.$4<256) return true;
}
return false;
}
15、检查输入字符串是否为空或者全部都是空格
1
2
3
4
5
6
export function isNull( str ){
if ( str == "" ) return true;
const regu = "^[ ]+$";
var const = new RegExp(regu);
return re.test(str);
}
16、判断是否是日期 (输入:date:日期;fmt:日期格式)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
export function isDate( date, fmt ) {
if (fmt==null) fmt="yyyyMMdd";
const yIndex = fmt.indexOf("yyyy");
if(yIndex==-1) return false;
const year = date.substring(yIndex,yIndex+4);
const mIndex = fmt.indexOf("MM");
if(mIndex==-1) return false;
const month = date.substring(mIndex,mIndex+2);
const dIndex = fmt.indexOf("dd");
if(dIndex==-1) return false;
const day = date.substring(dIndex,dIndex+2);
if(!isNumber(year)||year>"2100" || year< "1900") return false;
if(!isNumber(month)||month>"12" || month< "01") return false;
if(day>getMaxDay(year,month) || day< "01") return false;
return true;
}
17、字符1是否以字符串2开始和结束
1
2
3
4
5
6
7
8
9
10
function isFirstMatch(str1,str2){
const index = str1.indexOf(str2);
if(index==0) return true;
return false;
}
export function isLastMatch(str1,str2){
const index = str1.lastIndexOf(str2);
if(str1.length==index+str2.length) return true;
return false;
}
-------------本文结束感谢您的阅读-------------