C# 常用类型校验Validate
using System.Text;
using System.Text.RegularExpressions; namespace 落地页测试代码
{
public class Validate
{
private static Regex RegNumber = new Regex("^[0-9]+$");
private static Regex RegNumberSign = new Regex("^[+-]?[0-9]+$");
private static Regex RegDecimal = new Regex("^[0-9]+[.]?[0-9]+$");
private static Regex RegDecimalSign = new Regex("^[+-]?[0-9]+[.]?[0-9]+$"); //等价于^[+-]?\d+[.]?\d+$
private static Regex RegEmail = new Regex(@"^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样
private static Regex RegCHZN = new Regex("[\u4e00-\u9fa5]"); #region 用户名密码格式 /// <summary>
/// 返回字符串真实长度, 1个汉字长度为2
/// </summary>
/// <returns>字符长度</returns>
public static int GetStringLength(string stringValue)
{
return Encoding.Default.GetBytes(stringValue).Length;
} /// <summary>
/// 检测用户名格式是否有效
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public static bool IsValidUserName(string userName)
{
int userNameLength = GetStringLength(userName);
if (userNameLength >= && userNameLength <= && Regex.IsMatch(userName, @"^([\u4e00-\u9fa5A-Za-z_0-9]{0,})$"))
{ // 判断用户名的长度(4-20个字符)及内容(只能是汉字、字母、下划线、数字)是否合法
return true;
}
else
{
return false;
}
} /// <summary>
/// 密码有效性
/// </summary>
/// <param name="password"></param>
/// <returns></returns>
public static bool IsValidPassword(string password)
{
return Regex.IsMatch(password, @"^[A-Za-z_0-9]{6,16}$");
}
#endregion #region 数字字符串检查 /// <summary>
/// int有效性
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
static public bool IsValidInt(string val)
{
return Regex.IsMatch(val, @"^[1-9]\d*\.?[0]*$");
} /// <summary>
/// 是否数字字符串
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumber(string inputData)
{
Match m = RegNumber.Match(inputData);
return m.Success;
} /// <summary>
/// 是否数字字符串 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsNumberSign(string inputData)
{
Match m = RegNumberSign.Match(inputData);
return m.Success;
} /// <summary>
/// 是否是浮点数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimal(string inputData)
{
Match m = RegDecimal.Match(inputData);
return m.Success;
} /// <summary>
/// 是否是浮点数 可带正负号
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsDecimalSign(string inputData)
{
Match m = RegDecimalSign.Match(inputData);
return m.Success;
} #endregion #region 中文检测 /// <summary>
/// 检测是否有中文字符
/// </summary>
/// <param name="inputData"></param>
/// <returns></returns>
public static bool IsHasCHZN(string inputData)
{
Match m = RegCHZN.Match(inputData);
return m.Success;
} /// <summary>
/// 检测含有中文字符串的实际长度
/// </summary>
/// <param name="str">字符串</param>
public static int GetCHZNLength(string inputData)
{
System.Text.ASCIIEncoding n = new System.Text.ASCIIEncoding();
byte[] bytes = n.GetBytes(inputData); int length = ; // l 为字符串之实际长度
for (int i = ; i <= bytes.Length - ; i++)
{
if (bytes[i] == ) //判断是否为汉字或全脚符号
{
length++;
}
length++;
}
return length; } #endregion #region 常用格式 /// <summary>
/// 验证身份证是否合法 15 和 18位两种
/// </summary>
/// <param name="idCard">要验证的身份证</param>
public static bool IsIdCard(string idCard)
{
if (string.IsNullOrEmpty(idCard))
{
return false;
} if (idCard.Length == )
{
return Regex.IsMatch(idCard, @"^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$");
}
else if (idCard.Length == )
{
return Regex.IsMatch(idCard, @"^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[A-Z])$", RegexOptions.IgnoreCase);
}
else
{
return false;
}
} /// <summary>
/// 是否是邮件地址
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public static bool IsEmail(string inputData)
{
Match m = RegEmail.Match(inputData);
return m.Success;
} /// <summary>
/// 邮编有效性
/// </summary>
/// <param name="zip"></param>
/// <returns></returns>
public static bool IsValidZip(string zip)
{
Regex rx = new Regex(@"^\d{6}$", RegexOptions.None);
Match m = rx.Match(zip);
return m.Success;
} /// <summary>
/// 固定电话有效性
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public static bool IsValidPhone(string phone)
{
Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$", RegexOptions.None);
Match m = rx.Match(phone);
return m.Success;
} /// <summary>
/// 手机有效性
/// </summary>
/// <param name="strMobile"></param>
/// <returns></returns>
public static bool IsValidMobile(string mobile)
{
Regex rx = new Regex(@"^(13|15|17|18|19)\d{9}$", RegexOptions.None);
Match m = rx.Match(mobile);
return m.Success;
} /// <summary>
/// 电话有效性(固话和手机 )
/// </summary>
/// <param name="strVla"></param>
/// <returns></returns>
public static bool IsValidPhoneAndMobile(string number)
{
Regex rx = new Regex(@"^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$|^(13|15)\d{9}$", RegexOptions.None);
Match m = rx.Match(number);
return m.Success;
} /// <summary>
/// Url有效性
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
static public bool IsValidURL(string url)
{
return Regex.IsMatch(url, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$");
} /// <summary>
/// IP有效性
/// </summary>
/// <param name="ip"></param>
/// <returns></returns>
public static bool IsValidIP(string ip)
{
return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
} /// <summary>
/// domain 有效性
/// </summary>
/// <param name="host">域名</param>
/// <returns></returns>
public static bool IsValidDomain(string host)
{
Regex r = new Regex(@"^\d+$");
if (host.IndexOf(".") == -)
{
return false;
}
return r.IsMatch(host.Replace(".", string.Empty)) ? false : true;
} /// <summary>
/// 判断是否为base64字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsBase64String(string str)
{
return Regex.IsMatch(str, @"[A-Za-z0-9\+\/\=]");
} /// <summary>
/// 验证字符串是否是GUID
/// </summary>
/// <param name="guid">字符串</param>
/// <returns></returns>
public static bool IsGuid(string guid)
{
if (string.IsNullOrEmpty(guid))
return false; return Regex.IsMatch(guid, "[A-F0-9]{8}(-[A-F0-9]{4}){3}-[A-F0-9]{12}|[A-F0-9]{32}", RegexOptions.IgnoreCase);
} #endregion #region 日期检查 /// <summary>
/// 判断输入的字符是否为日期
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static bool IsDate(string strValue)
{
return Regex.IsMatch(strValue, @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))");
} /// <summary>
/// 判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
/// </summary>
/// <param name="strValue"></param>
/// <returns></returns>
public static bool IsDateHourMinute(string strValue)
{
return Regex.IsMatch(strValue, @"^(19[0-9]{2}|[2-9][0-9]{3})-((0(1|3|5|7|8)|10|12)-(0[1-9]|1[0-9]|2[0-9]|3[0-1])|(0(4|6|9)|11)-(0[1-9]|1[0-9]|2[0-9]|30)|(02)-(0[1-9]|1[0-9]|2[0-9]))\x20(0[0-9]|1[0-9]|2[0-3])(:[0-5][0-9]){1}$");
} #endregion #region 其他 /// <summary>
/// 检查字符串最大长度,返回指定长度的串
/// </summary>
/// <param name="sqlInput">输入字符串</param>
/// <param name="maxLength">最大长度</param>
/// <returns></returns>
public static string CheckMathLength(string inputData, int maxLength)
{
if (inputData != null && inputData != string.Empty)
{
inputData = inputData.Trim();
if (inputData.Length > maxLength)//按最大长度截取字符串
{
inputData = inputData.Substring(, maxLength);
}
}
return inputData;
} /// <summary>
/// 转换成 HTML code
/// </summary>
/// <param name="str">string</param>
/// <returns>string</returns>
public static string Encode(string str)
{
str = str.Replace("&", "&");
str = str.Replace("'", "''");
str = str.Replace("\"", """);
str = str.Replace(" ", " ");
str = str.Replace("<", "<");
str = str.Replace(">", ">");
str = str.Replace("\n", "<br>");
return str;
}
/// <summary>
///解析html成 普通文本
/// </summary>
/// <param name="str">string</param>
/// <returns>string</returns>
public static string Decode(string str)
{
str = str.Replace("<br>", "\n");
str = str.Replace(">", ">");
str = str.Replace("<", "<");
str = str.Replace(" ", " ");
str = str.Replace(""", "\"");
return str;
} #endregion
}
}
C# 常用类型校验Validate的更多相关文章
- ashx中Response.ContentType的常用类型
ashx中Response.ContentType的常用类型: text/plaintext/htmltext/xmlapplication/jsonimage/GIFapplication/x-cd ...
- 【跟着子迟品 underscore】常用类型判断以及一些有用的工具方法
Why underscore 最近开始看 underscore.js 源码,并将 underscore.js 源码解读 放在了我的 2016 计划中. 阅读一些著名框架类库的源码,就好像和一个个大师对 ...
- MyBatis jdbcType常用类型
MyBatis jdbcType常用类型 jdbcType与javaType对应关系 javaType jdbctype CHAR String VARCHAR String LONGVARCHAR ...
- Underscore.js 常用类型判断以及一些有用的工具方法
1. 常用类型判断以及一些有用的工具方法 underscore.js 中一些 JavaScript 常用类型检查方法,以及一些工具类的判断方法. 首先我们先来谈一谈数组类型的判断.先贴出我自己封装好的 ...
- c++之 常用类型
C/C++常用类型的范围 C/C++里常用的类型及表示范围如下表所示: 类型 sizeof 表示范围 说明 char 1 -128 - 127 -2^7 - (2^7 - 1) short 2 -32 ...
- JS常用数据校验集合(adding)
常用数据校验集合 var _validator = { MAIL_REGEX: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,; ...
- 通过后缀名和MIME-TYPE检查实现文件类型校验
前言 文件上传是一个在开发中很常见的需求场景,通常出于安全考虑,我们会对上传的文件进行类型校验,其中常见的有后缀名校验,mime-type校验 话不多说,直接上代码 1.首先定义允许上传的文件类型白名 ...
- js常用身份校验规则
js常用身份校验规则 var Validator = { extractBirth: function(id) { // 身份证提取出生年月 var re = null, split, year, m ...
- 04-MySQL的存储引擎和列的常用类型
1. MySQL中的数据库分类 2. MySQL中的存储引擎 MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并 ...
随机推荐
- java返回数据工具类
import com.qbskj.project.util.SpringUtils; /** * 消息 * */ public class Message { /** * 类型 */ public e ...
- mysql数据库中插入数据INSERT INTO SET的优势
往mysql数据库中插入数据.以前常用 INSERT INTO 表名 (列名1,列名2…) VALUES(列值1,列值2); 如果在PHP程序中,就会写成如下示例(往商品库里增加商品) $sql = ...
- P1119 灾后重建(floyd进阶)
思路:这道题看n的范围很小(n<=200),显然就用floyd可以解决的问题,但又并不是简单的floyd算法,还是需要一些小小的变化.一开始我的思路是先跑一次弗洛伊德最短路,这样子显然复杂度很高 ...
- mysql8.0 Server 在Windows平台中的安装、初始化和远程访问设置
mysql8.0 server安装 1.下载mysql 8.0 可以到mysql官网下载 https://dev.mysql.com/downloads/mysql 或者如下地址 mysql-8.0. ...
- Fiddle Proxy
1.抓包原理 Fiddler是类似代理服务器的形式工作,它能够记录所有你的电脑和互联网之间的http(S)通讯,可以查看.修改所有的“进出”的数据.使用代理地址:127.0.0.1, 默认端口:888 ...
- ftp服务器安装与配置
ftp服务器安装与配置 1. ftp服务端的安装 1 如果之前配置过ftp服务器的还是之后配置的服务器,无法启动服务,那么基本是配置出现了错误,那么可先完全卸载后再进行安装.如果无法定位多半是镜像源的 ...
- 我的python之路
一.基础语法 Python基础—基本语法结构 Python基础—程序控制结构 Python基础—基本数据类型 Python基础—文件的读写操作 二.函数 Python基础—初识函数 Python基础— ...
- python并发编程之多线程基础知识点
1.线程理论知识 概念:指的是一条流水线的工作过程的总称,是一个抽象的概念,是CPU基本执行单位. 进程和线程之间的区别: 1. 进程仅仅是一个资源单位,其中包含程序运行所需的资源,而线程就相当于车间 ...
- 观察者模式 vs 发布-订阅模式
我曾经在面试中被问道,_“观察者模式和发布订阅模式的有什么区别?” _我迅速回忆起“Head First设计模式”那本书: 发布 + 订阅 = 观察者模式 “我知道了,我知道了,别想骗我” 我微笑着回 ...
- cookie session token
HTTP HTTP是无状态(stateless)的网络协议.HTTP协议自身不对请求和响应之间的通信状态进行保存.也就是说HTTP协议对于发送过的请求或响应都不做持久化处理. 为了实现保持状态功 ...