-
C#教程之C#教程之C# 常用验证
本站最新发布 C#从入门到精通
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
试听地址 https://www.xin3721.com/eschool/CSharpxin3721/
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Web.UI.HtmlControls; using System.Web.UI; using System.Text.RegularExpressions; namespace Common { public class Validate { private static readonly Regex RegPhone = new Regex("(^(\\d{11})$|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)"); //电话号码和手机验证 private static Regex RegEmail = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@([\\w-]+\\.)+\\w{2,3})\\s*$"); //new Regex("^[\\w-]+@[\\w-]+\\.(com|net|org|edu|mil|tv|biz|info)$");//w 英文字母或数字的字符串,和 [a-zA-Z0-9] 语法一样 private static Regex RegNum = new Regex("^[0-9]+$"); //必须是数字正则表达式(小数或整数) private static Regex regex = new Regex(@"/^[0-9]+\.?[0-9]{0,3}$/"); /// <summary> /// 身份证正值表达式 /// </summary> private static readonly Regex RegCardId = new Regex("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)"); #region 确定用户输入是否合法 /// <summary> /// 确定用户输入是否合法 /// </summary> /// <param name="text">用户输入字符串</param> /// <param name="maxLength">最大字符串长度</param> /// <returns></returns> public static string InputText(string text, int maxLength) { if (string.IsNullOrEmpty(text)) return string.Empty; text = text.Trim(); if (maxLength != 0) if (text.Length > maxLength) text = text.Substring(0, maxLength); text = Regex.Replace(text, "[\\s]{2,}", " "); text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|\\n)*?>)", "\n"); text = Regex.Replace(text, "(\\s*&[n|N][b|B][s|S][p|P];\\s*)+", " "); //text = Regex.Replace(text, "<(.|\\n)*?>", string.Empty); //屏蔽标签 text = text.Replace("'", "''"); return text; } #endregion #region 验证电话号码 // 电话号码和手机号码检查 /// <summary> /// 电话号码和手机号码检查 /// </summary> /// <param name="inputData">电话号码或手机号码</param> /// <returns>匹配结果</returns> public static bool IsPhone(string inputData) { Match m = RegPhone.Match(inputData); return m.Success; } #endregion #region 验证参数是否为中文字符 /// <summary> /// 验证参数是否为中文字符 /// </summary> /// <param name="input">输入参数</param> /// <returns></returns> public static bool IsChinese(string input) { Regex regex = new Regex(@"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase); return regex.IsMatch(input); } #endregion #region 邮件地址 /// <summary> /// 邮件地址验证 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns>验证结果</returns> public static bool IsEmail(string inputData) { Match m = RegEmail.Match(inputData); return m.Success; } #endregion #region 是否为数字 /// <summary> /// 是否为数字 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns>是否为数字</returns> public static bool IsNum(string inputData) { if(string.IsNullOrEmpty(inputData)) { return false; } Match m = RegNum.Match(inputData); return m.Success; } /// <summary> /// 判断是否是整数或小数 /// </summary> /// <param name="str">输入的字符串</param> /// <returns>是否为数字</returns> public static bool IsNumAll(string str) { if (string.IsNullOrEmpty(str)) { return false; } Match m = regex.Match(str); return m.Success; } #endregion #region 是否为身份证 /// <summary> /// 是否为身份证 /// </summary> /// <param name="inputData">输入字符串</param> /// <returns>是否为身份证</returns> public static bool IsCardId(string inputData) { Match m = RegCardId.Match(inputData); return m.Success; } #endregion /// <summary> /// 判断字符串是否是纯数字 /// </summary> /// <param name="message">源字符串</param> /// <returns></returns> public static bool IsNumberic(string message)//, out int result { System.Text.RegularExpressions.Regex rex = new System.Text.RegularExpressions.Regex(@"^\d+$"); var result = -1; if (rex.IsMatch(message)) { result = int.Parse(message); return true; } else return false; } } }
栏目列表
最新更新
如何使用OS模块中的stat方法
Python os 模块
seek() 方法
python打开文件实例1
Python写入文件
什么是流?
文件操作如何进制逐行读取
Python相对路径
with创建临时运行环境
Python文件操作
.Net Standard(.Net Core)实现获取配置信息
Linux PXE + Kickstart 自动装机
Shell 编程 基础
Shell 编程 条件语句
CentOS8-网卡配置及详解
Linux中LVM逻辑卷管理
1.数码相框-相框框架分析(1)
Ubuntu armhf 版本国内源
Linux中raid磁盘阵列
搭建简易网站
access教程之Access简介
mysql 安装了最新版本8.x版本后的报错:
Mysql空间数据&空间索引(spatial)
如何远程连接SQL Server数据库的图文教程
复制SqlServer数据库的方法
搜索sql语句
sql中返回参数的值
sql中生成查询的模糊匹配字符串
数据定义功能
数据操作功能