前置
1. IPaddr 地址验证插件 需要安装 npm i ipaddr.js
2. 字符长度验证
export function regCode(str: string, len: number) {
let i: number = 0;
let totalLength: number = 0;
/\* 计算utf-8编码情况下的字符串长度 \*/
for (i = 0; i < str.length; i++) {
if (str.charCodeAt(i) <= parseInt('0x7F')) {
totalLength += 1;
} else if (str.charCodeAt(i) <= parseInt('0x7FF')) {
totalLength += 2;
} else if (str.charCodeAt(i) <= parseInt('0xFFFF')) {
totalLength += 3;
} else if (str.charCodeAt(i) <= parseInt('0x1FFFFF')) {
totalLength += 4;
} else if (str.charCodeAt(i) <= parseInt('0x3FFFFFF')) {
totalLength += 5;
} else {
totalLength += 6;
}
}
if (totalLength > len) {
return false;
} else {
return true;
}
}
ip地址 字符长度 端口号正则校验 typescript, element-ui
import { checkEmail, checkPhone, isValidIP, regCode } from '@/utils/index';
import IPaddr from 'ipaddr.js';
/\*\*
\* validate device name
\* 验证设备名称
\* max 2-20个字符 | required
\*/
export function validateDeviceName(rule: any, value: any, callback: any): any {
if (value == '') {
callback('请输入设备名称');
} else if (regCode(value, 1) || !regCode(value, 20)) {
callback('设备名称为2-20个字符');
} else {
callback();
}
}
/\*\*
\* validate device desc
\* 描述
\* max 200个字符 | 非必填
\*/
export function validateDeviceDesc(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else if (!regCode(value, 200)) {
callback('最大输入为200个字符');
}
}
/\*\*
\* 风险规则校验 {描述、补救建议}
\*/
export function validateDeviceDesc500(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else if (!regCode(value, 500)) {
callback('最大输入为500个字符');
}
}
/\*\*
\* validate IPv4
\* 验证IPv4地址
\* required
\*/
export function validateIPv4(rule: any, value: any, callback: any): any {
if (value == '') {
callback('IP不能为空');
} else if (isValidIP(value)) {
callback('请输入正确的IPv4地址');
} else if (!IPaddr.IPv4.isIPv4(value)) {
callback('请输入正确的IPv4地址');
} else {
callback();
}
}
/\*\*
\* validate port
\* 验证端口号
\* 1 ~ 65535 required
\*/
export function validatePort(rule: any, value: any, callback: any): any {
if (value == '') {
callback('端口号不能为空');
} else if (value > 65535 || value < 1) {
callback('请输入正确的端口号1-65535之间');
} else {
callback();
}
}
/\*\*
\* validate login name
\* 验证设备登录方式 用户名
\* max 100 required
\*/
export function validateDeviceLoginName(rule: any, value: any, callback: any): any {
if (value == '') {
callback('用户名不能为空');
} else if (!regCode(value, 100)) {
callback('用户名最大可输入100个字符');
} else {
callback();
}
}
/\*\*
\* validate email
\* 验证邮箱
\* max 30
\* email has @
\*/
export function validateEmail(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else {
if (checkEmail(value)) {
callback('请输入正确的邮箱格式');
} else if (!regCode(value, 30)) {
callback('邮箱最大可输入30个字符');
} else {
callback();
}
}
}
/\*\*
\* validate phone
\* 验证手机号
\* max 30
\* phone length 11
\*/
export function validatePhone(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else {
if (checkPhone(value)) {
callback('请输入正确的手机号');
} else if (!regCode(value, 30)) {
callback('手机号最大可输入30个字符');
} else {
callback();
}
}
}
/\*\*
\* validate code length
\* 验证字符长度
\* max 30
\*/
export function validateMax30(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else {
if (!regCode(value, 30)) {
callback('最大可输入30个字符');
} else {
callback();
}
}
}
/\*\*
\* 风险规则校验名称
\*/
export function validateMax50(rule: any, value: any, callback: any): any {
if (value == '') {
callback();
} else {
if (!regCode(value, 50)) {
callback('最大可输入50个字符');
} else {
callback();
}
}
}
/\*\*
\* validate ip
\* 验证IP 支持如下
\* 1.1.1.1
\* 2.2.2.0/24
\* 3.3.3.10-3.3.3.20
\* any
\*/
export function validateIp(rule: any, value: any, callback: any): any {
if (value === '') {
callback('IP地址不能为空');
} else if (value.split('\n').length > 100) {
callback();
} else {
if (!validateIPAddr(value)) {
callback('请输入正确的IP地址');
} else {
callback();
}
}
}
export function validateIPAddr(value: string) {
let reg = /(^(\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])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$)|(^any$)|(^(\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])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\/(\d|[0-2]\d|3[0-2])$)|(^(\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])\.(\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])\.(\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])$)/;
let len = value.split('\n');
for (let i of len) {
if (!reg.test(i)) {
return false;
break;
}
}
return true;
}
/\*\*
\* validate ip
\* 验证端口号 支持如下
\* 目的端口 { TCP | UDP } 1-65535 ==> targetPort
\* 80
\* 443
\* 8080-9090
\*
\* 目的端口 { OTHER } 0-255 ==> targetOthe
\* 15
\* 125
\* 133-155
\*/
export function validateTargetPort(value: string) {
/\*\*
\* 1. 4位及以下最大 9999 \d{4}
\* 2. 5位 59999 [0-5]\d{4}
\* 3. 5位 60000 - 65535 [0-6]
\*/
let reg = /(^(\d{4}|[0-5]\d{4}|[0-6][0-5]{2}[0-3][0-5])$)|(^(\d{4}|[0-5]\d{4}|[0-6][0-5]{2}[0-3][0-5])-(\d{4}|[0-5]\d{4}|[0-6][0-5]{2}[0-3][0-5])$)|(^any$)/;
let len = value.split('\n');
for (let i of len) {
if (!reg.test(i)) {
return false;
break;
}
}
return true;
}
export function validateTargetOther(value: string) {
let reg = /(^(\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])$))|(^any$)/;
let len = value.split('\n');
for (let i of len) {
if (!reg.test(i)) {
return false;
break;
}
}
return true;
}
/\*\*
\* validate ipv4 or URL
\* 验证邮箱或域名
\* IP { 1.1.1.1 }
\* 域名 {
\* http://dev.360es.com
\* https://test-dev.360es.com
\* }
\*/
export function validateIPv4orURL(rule: any, value: any, callback: any): any {
if (value == '') {
callback('请输入邮件服务器IP或域名');
} else {
let reg = /(^(\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])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$)|(^(http|https|HTTP|HTTPS):\/\/([\w.-]+\/?)\.([\w.]+\/?)\.([\w.]+\/?)\S\*)/;
if (!reg.test(value)) {
callback('请输入正确的服务器IP或域名');
} else {
callback();
}
}
}
/\*\*
\* validate addr or service
\* 地址对象名,
\* 服务对象名
\*/
export function validateMin1Max63(rule: any, value: any, callback: any): any {
if (value == '') {
callback('对象名不能为空');
} else if (!regCode(value, 63)) {
callback('最大可输入63个字符');
} else {
callback();
}
}
验证IP地址 子网 网段 网段对比
/\*\*
\* ip 地址验证
\*/
export function isValidIP(ip: string) {
let reg = /^(\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])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
return !reg.test(ip);
}
/\*\*
\* 验证子网
\*/
export function isVaildSubnet(value: string): boolean {
let regx = /(^(\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])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\/(\d|[0-2]\d|3[0-2])$)/;
return regx.test(value);
}
/\*\*
\* 验证网段
\*/
export function isVaildSegment(value: string): boolean {
let regx = /(^(\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])\.(\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])\.(\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])$)/;
return regx.test(value);
}
/\*\*
\* 网段校验比对
\* @param value
\*/
export function isValidSegmentComparison(value: string): boolean {
let start!: string[];
let end!: string[];
let source = value.split('-');
let valid!: boolean;
start = source[ 0 ].split('.');
end = source[ 1 ].split('.');
for (let i = 0; i < start.length; i++) {
const startElement = start[ i ];
if (Number(startElement) != Number(end[ i ])) {
if (Number(startElement) > Number(end[ i ])) {
valid = false;
break;
} else {
valid = true;
}
}
valid = true;
}
return valid;
}
邮箱
checkemail(t){
let reg = /^([a-zA-Z0-9\_-])+@([a-zA-Z0-9\_-])+((\.[a-zA-Z0-9\_-]{2,3}){1,2})$/;
return !reg.test(t);
}
checkemail ( v ) {
let re = /^\w+((-\w+)|(\.\w+))\*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)\*\.[A-Za-z0-9]+$/;
return !re.test(v)
}
座机号
checktphone(t){
let re = /^(0[1-9]\d{1,2}-)\d{6,7}$/;
return !re.test(t);
}
手机号
checkphone(t){
let re = /^1[0-9]{10}$/; //let reg = /^1[1-9]{2}[0-9]{8}$/g
return !re.test(t);
}
验证中文字符
/^[\u4e00-\u9fa5]+$/
匹配双字节字符(包括汉字在内):[^\x00-\xff]
验证数字字母汉字
/^[A-Za-z0-9\u4e00-\u9fa5]+$/
/\*\*
\* 验证数字字母下划线连接符
\* @param t
\*/
export function checkNumberASymbol(t: string) {
let reg = /^[A-Za-z0-9-\_]+$/;
return !reg.test(t);
}
/\*\*
\* 验证是否含有数字、字母、特殊字符
\* @param t
\*/
export function checkPassRxg(t: string) {
let reg1 = /[0-9]/;
let reg2 = /[A-Za-z]/;
let reg3 = /[\W]/;
let test = reg1.test(t) && reg2.test(t) && reg3.test(t);
return !test;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。