C# 數(shù)據(jù)驗(yàn)證Regex示例詳解
Regular Expression,簡(jiǎn)稱 Regex,是一種用于匹配和處理文本的強(qiáng)大工具。它通過(guò)定義特定的模式,可以用來(lái)搜索、替換或提取字符串中的特定內(nèi)容。
先引入命名空間
using System.Text.RegularExpressions;
Intege(整數(shù))
必須是正整數(shù)
//必須是正整數(shù)
public static bool IsPositiveInteger(string txt)
{
Regex objReg = new Regex(@"^[1-9]\d*$");
return objReg.IsMatch(txt);
}
正整數(shù)和零
public static bool IsPositiveIntegerAndZero(string txt)
{
Regex objReg = new Regex(@"^[1-9]\d*|0$");
return objReg.IsMatch(txt);
}
負(fù)整數(shù)
public static bool IsNegativeInteger(string txt)
{
Regex objReg = new Regex(@"^-[1-9]\d*$");
return objReg.IsMatch(txt);
}正負(fù)均可
public static bool IsInteger(string txt)
{
Regex objReg = new Regex(@"^-?[1-9]\d*$");
return objReg.IsMatch(txt);
}Decimal(小數(shù))
正數(shù)
public static bool IsPositiveDecimal(string txt)
{
Regex objReg = new Regex(@"^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$");
return objReg.IsMatch(txt);
}負(fù)數(shù)
public static bool IsNegativeDecimal(string txt)
{
Regex objReg = new Regex(@"^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$");
return objReg.IsMatch(txt);
}
正負(fù)均可
public static bool IsDecimal(string txt)
{
Regex objReg = new Regex(@"^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$");
return objReg.IsMatch(txt);
}其他驗(yàn)證
郵箱
public static bool IsEmail(string txt)
{
Regex objReg = new Regex(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
return objReg.IsMatch(txt);
}身份證
public static bool IsIdentityCard(string txt)
{
Regex objReg = new Regex(@"^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$");
return objReg.IsMatch(txt);
}郵箱編碼
public static bool IsPostalCode(string txt)
{
if (txt.Length != 6) return false;
Regex objReg = new Regex(@"[1-9]\d{5}(?!\d)");
return objReg.IsMatch(txt);
}到此這篇關(guān)于C# 數(shù)據(jù)驗(yàn)證Regex的文章就介紹到這了,更多相關(guān)C# 數(shù)據(jù)驗(yàn)證Regex內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在WPF中合并兩個(gè)ObservableCollection集合
這篇文章介紹了在WPF中合并兩個(gè)ObservableCollection集合的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
C#比較二個(gè)數(shù)組并找出相同或不同元素的方法
這篇文章主要介紹了C#比較二個(gè)數(shù)組并找出相同或不同元素的方法,涉及C#針對(duì)數(shù)組的交集、補(bǔ)集等集合操作相關(guān)技巧,非常簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用
這篇文章主要為大家介紹了Unity?UGUI的CanvasScaler畫布縮放器組件介紹使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
C# 對(duì)Outlook2010進(jìn)行二次開發(fā)的圖文教程
下面小編就為大家分享一篇C# 對(duì)Outlook2010進(jìn)行二次開發(fā)的圖文教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
C#使用MSTest進(jìn)行單元測(cè)試的示例代碼
MSTest是微軟官方提供的.NET平臺(tái)下的單元測(cè)試框架,這篇文章主要為大家詳細(xì)介紹了C#如何使用MSTest進(jìn)行單元測(cè)試,感興趣的小伙伴可以參考一下2023-12-12

