• 解析html成 普通文本
    转换成 HTML code
    检查字符串最大长度,返回指定长度的串
    判断输入的字符是否为日期,如2004-07-12 14:25|||1900-01-01 00:00|||9999-12-31 23:59
    判断输入的字符是否为日期
    验证字符串是否是GUID
    判断是否为base64字符串
    domain 有效性
    IP有效性
    Url有效性
    电话有效性(固话和手机 )
    固定电话有效性
    邮编有效性
    是否是邮件地址
    验证身份证是否合法 15 和 18位两种
    检测含有中文字符串的实际长度
    检测是否有中文字符
    是否是浮点数 可带正负号
    是否是浮点数
    是否数字字符串 可带正负号
    int有效性
    密码有效性
    检测用户名格式是否有效
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using Yestar.Util.Extension;
using System;
using System.Text.RegularExpressions;namespace Yestar.Util
{/// <summary>/// 各种输入格式验证辅助类/// </summary>public class ValidateUtil{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 >= 4 && userNameLength <= 20 && 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 = 0; // l 为字符串之实际长度 for (int i = 0; i <= bytes.Length - 1; i++){if (bytes[i] == 63) //判断是否为汉字或全脚符号 {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 == 15){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 == 18){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(".") == -1){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(0, maxLength);}}return inputData;}/// <summary>/// 转换成 HTML code/// </summary>/// <param name="str">string</param>/// <returns>string</returns>public static string Encode(string str){str = str.Replace("&", "&amp;");str = str.Replace("'", "''");str = str.Replace("\"", "&quot;");str = str.Replace(" ", "&nbsp;");str = str.Replace("<", "&lt;");str = str.Replace(">", "&gt;");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("&gt;", ">");str = str.Replace("&lt;", "<");str = str.Replace("&nbsp;", " ");str = str.Replace("&quot;", "\"");return str;}#endregion}
}

.net程序末法时代,人人贡献一点开源产品
源代码下载: http://www.zhruanjian.com/
技术交流群:345265775

.net 后台数据校验,电话号码,邮箱地址,IP有效性相关推荐

  1. python使用正则表达式验证邮箱地址语法有效性

    python使用正则表达式验证邮箱地址语法有效性 #python使用正则表达式验证邮箱地址语法有效性 import re # mail regular expression formula# rege ...

  2. SpringMVC后台数据校验

    第①步:引入依赖 <!--数据校验--><dependency><groupId>org.hibernate</groupId><artifact ...

  3. 如何使用JSR 303 进行后台数据校验?

    文章目录 一.JSR 303 1.什么是 JSR 303? 2.为什么使用 JSR 303? 3.JSR 303 常见操作? 二.使用 JSR 303 相关注解处理逻辑 1.JSR 303 注解处理逻 ...

  4. Struts2 类型转换和数据校验

    学习内容 Struts 2类型转换 Struts 2数据校验 能力目标 熟悉Struts 2自带类型转换器 能自定义类型转换器 自定义输入校验功能 熟悉Struts 2内置校验器 本章简介 Strut ...

  5. JSR303数据校验方法

    使用JSR303做后台数据校验是有效确保非法数据绕过前端提交数据,必须确保数据的有效性和准确性.方法步骤如下: 一.引入依赖 <dependency><groupId>org. ...

  6. SpringMVC_数据校验

    一:数据校验的分类 数据校验分为前台校验和后台校验,前台校验主要是利用JavaScript来完成的,后台校验主要利用Java代码来完成的,可以使用SpringMvc和Hibernate validat ...

  7. 验证邮箱地址和手机号码

    namespace test { class Test5 { static void Main(string[] args) { Console.Write("请输入要验证的手机号码:&qu ...

  8. 数据校验(身份证,ip地址,银行卡号等)

    数据校验正则记录: public class DataValidateUtil {public static boolean validateRiskInfo(String dataType,Stri ...

  9. Vue 的身份证 手机号码 电话号码 邮箱等校验

    /** @desc 基于ElementUI Form rules 进行使用, 参考官方文档: http://element-cn.eleme.io/#/zh-CN/component/form [自定 ...

最新文章

  1. ICML 2020 | Google提出最强生成式摘要预训练模型——天马
  2. M2Crypto/__m2crypto.so: undefined symbol: SSLv3_method
  3. jms mysql_JMS学习九(ActiveMQ的消息持久化到Mysql数据库)
  4. 转:min(x,y)高效算法
  5. Android实现记账本(麻雀虽小,五脏俱全)
  6. Robot Framework自动化测试用具 Wait Until Keyword Succeeds关键字使用案例
  7. 《Tableau数据可视化从入门到精通》之读取PDF文件数据
  8. 2020伊始,我结束了人生的两个轮回
  9. js+HTML实现组织结构图
  10. 矩阵相关知识回顾--协方差的意义
  11. GO富集分析可视化——柱形图和棒棒糖图
  12. 闵行区科技小巨人工程申请条件及奖励政策解读
  13. win7设置密码次数超限技巧
  14. 2021-07-05 Ubuntu18安装Caffe-ssd with GPU并运行Apollo mutitask yolo3d
  15. Mysql入门教程--基础命令
  16. C语言控制台程序中添加背景图片
  17. .Net5.0 微服务之服务注册与发现(Consul)
  18. 年轻貌美的小姐姐加我微信后竟要求我帮她做这种事...
  19. Unity多屏/多显示器 显示策略
  20. 【NLP】学不会打我 半小时学会基本操作 14 文本处理 <PAD><EOS><UNK><GO>

热门文章

  1. iOS UIColor RGB 颜色对照表
  2. 这是我独有的排解方式
  3. 谷歌adwords—点击诱饵被拒登
  4. 吐槽一下一些垃圾网站
  5. vue element ui 上传图片压缩
  6. 天下无云第二步,逐像素图片合成
  7. 粤港澳高速船电子海圈更新实践
  8. 学java 安卓还是ios开发_非计算机科班出身,有JAVA基础,问学安卓开发还是IOS开发好些?...
  9. aoa计算机二级word单项操作,计算机二级AOA word单项操作
  10. [MODIS数据处理#9]例四:基于MCD12Q2数据集初步分析中国植被物候空间分布特征