C# 如何验证字符串,使其仅允许包含字母数字字符?
如何使用正则表达式验证字符串,使其仅允许包含字母数字字符C# 如何验证字符串,使其仅允许包含字母数字字符?,c#,regex,C#,Regex,如何使用正则表达式验证字符串,使其仅允许包含字母数字字符 (我也不想允许任何空格)。使用以下表达式: ^[a-zA-Z0-9]*$ 即: ^\w+$将允许a-zA-Z0-9 使用^[a-zA-Z0-9]+$禁止下划线 请注意,这两种方法都要求字符串不为空。使用*而不是+允许使用空字符串。您可以使用扩展函数而不是正则表达式轻松完成此操作 public static bool IsAlphaNum(this string str) { if (string.IsNullOrEmpty(st
(我也不想允许任何空格)。使用以下表达式:
^[a-zA-Z0-9]*$
即:
^\w+$
将允许a-zA-Z0-9
使用^[a-zA-Z0-9]+$
禁止下划线
请注意,这两种方法都要求字符串不为空。使用
*
而不是+
允许使用空字符串。您可以使用扩展函数而不是正则表达式轻松完成此操作
public static bool IsAlphaNum(this string str)
{
if (string.IsNullOrEmpty(str))
return false;
for (int i = 0; i < str.Length; i++)
{
if (!(char.IsLetter(str[i])) && (!(char.IsNumber(str[i]))))
return false;
}
return true;
}
虽然我认为基于regex的解决方案可能是我的选择,但我还是想把它封装在一个类型中
public class AlphaNumericString
{
public AlphaNumericString(string s)
{
Regex r = new Regex("^[a-zA-Z0-9]*$");
if (r.IsMatch(s))
{
value = s;
}
else
{
throw new ArgumentException("Only alphanumeric characters may be used");
}
}
private string value;
static public implicit operator string(AlphaNumericString s)
{
return s.value;
}
}
现在,当您需要一个经过验证的字符串时,您可以让方法签名需要一个AlphaNumericString,并且知道如果您得到一个,它是有效的(除了null)。如果有人试图传入未经验证的字符串,它将生成一个编译器错误
如果您愿意,您可以使用fancier并实现所有相等运算符,或者从纯ol'字符串显式转换为AlphaNumericString。在.NET 4.0中,您可以使用LINQ:
if (yourText.All(char.IsLetterOrDigit))
{
//just letters and digits.
}
yourText.All
将停止执行并返回false
第一次char.IsleterOrdigit
报告false
,因为All
的合同无法履行
注意此答案不严格检查字母数字(通常是A-Z、A-Z和0-9)。这个答案允许使用像åäö
这样的本地字符
更新2018-01-29
上述语法仅在使用具有正确类型的单个参数(在本例中为char
)的单个方法时有效
要使用多个条件,您需要这样编写:
if (yourText.All(x => char.IsLetterOrDigit(x) || char.IsWhiteSpace(x)))
{
}
我需要检查A-Z,A-Z,0-9;没有正则表达式(即使OP要求正则表达式) 在这里混合各种答案和评论,以及讨论,测试字母或数字,避免使用其他语言字母,避免使用分数字符等其他数字
if (!String.IsNullOrEmpty(testString)
&& testString.All(c => Char.IsLetterOrDigit(c) && (c < 128)))
{
// Alphanumeric.
}
if(!String.IsNullOrEmpty(testString)
&&testString.All(c=>Char.isleterordigit(c)&(c<128)))
{
//字母数字。
}
为了检查字符串是否同时是字母和数字的组合,您可以使用.NET 4.0和LINQ按如下方式重新编写@jgauffin answer:
if(!string.IsNullOrWhiteSpace(yourText) &&
yourText.Any(char.IsLetter) && yourText.Any(char.IsDigit))
{
// do something here
}
我建议不要依赖于.NET framework中现成的和内置的代码,尝试提出新的解决方案..这就是我所做的
public bool isAlphaNumeric(string N)
{
bool YesNumeric = false;
bool YesAlpha = false;
bool BothStatus = false;
for (int i = 0; i < N.Length; i++)
{
if (char.IsLetter(N[i]) )
YesAlpha=true;
if (char.IsNumber(N[i]))
YesNumeric = true;
}
if (YesAlpha==true && YesNumeric==true)
{
BothStatus = true;
}
else
{
BothStatus = false;
}
return BothStatus;
}
public bool isAlphaNumeric(字符串N)
{
bool-YesNumeric=false;
bool-YesAlpha=假;
bool-BothStatus=false;
对于(int i=0;i
根据克莱特斯的回答,您可以创建新的扩展
public static class StringExtensions
{
public static bool IsAlphaNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
return false;
Regex r = new Regex("^[a-zA-Z0-9]*$");
return r.IsMatch(str);
}
}
答案和我的一样
如果需要非正则ASCII
a-z 0-9
检查,则不能使用char.isleterOrdigit()
,因为这包括其他Unicode字符。
您可以做的是检查字符代码范围
- 48->57是数字
- 65->90是大写字母
- 97->122是小写字母
public static bool IsAsciiAlphaNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
for (int i = 0; i < str.Length; i++)
{
if (str[i] < 48) // Numeric are 48 -> 57
{
return false;
}
if (str[i] > 57 && str[i] < 65) // Capitals are 65 -> 90
{
return false;
}
if (str[i] > 90 && str[i] < 97) // Lowers are 97 -> 122
{
return false;
}
if (str[i] > 122)
{
return false;
}
}
return true;
}
公共静态bool IsAsciiAlphaNumeric(此字符串str)
{
if(string.IsNullOrEmpty(str))
{
返回false;
}
对于(int i=0;i57
{
返回false;
}
如果(str[i]>57&&str[i]<65)//大写字母为65->90
{
返回false;
}
如果(str[i]>90&&str[i]<97)//降低值为97->122
{
返回false;
}
如果(str[i]>122)
{
返回false;
}
}
返回true;
}
虽然有很多方法可以剥下这只猫的皮,但我更喜欢将这些代码包装到可重用的扩展方法中,这样以后就可以轻松地完成。使用扩展方法时,还可以避免使用正则表达式,因为它比直接字符检查慢。我喜欢使用extensions.csnuget包中的扩展。它使该检查变得非常简单:
使用扩展;
”“smith23”.IsAlphaNumeric()
将返回True,而“smith 23”.IsAlphaNumeric(false)
将返回false。默认情况下,.IsAlphaNumeric()
方法忽略空格,但也可以如上所示覆盖它。如果希望允许空格“smith 23”.IsAlphaNumeric()
将返回True,则简单默认为argMyString.IsAlphaNumeric()
我想用javascript怎么样?如果你清理数据库名称或类似的内部代码,你就不会在意它是否在英语国家运行。我讨厌正则表达式。我知道我永远不会记得语法。“即使我研究它,很快也会有一天它会被完全忘记的。”菲尔。也许吧,但到那时我将委托给懂正则表达式的人;-)有人说,“如果你用正则表达式解决一个问题,那么你就有两个问题。”这可能是一个品味问题,但我会将循环表示为“foreach(char c in str){…}”。空字符串是否被认为是OK取决于应用程序,所以我将把它去掉。我也不会在这样的tr中填充6行空行
public static class StringExtensions
{
public static bool IsAlphaNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
return false;
Regex r = new Regex("^[a-zA-Z0-9]*$");
return r.IsMatch(str);
}
}
public static bool IsAsciiAlphaNumeric(this string str)
{
if (string.IsNullOrEmpty(str))
{
return false;
}
for (int i = 0; i < str.Length; i++)
{
if (str[i] < 48) // Numeric are 48 -> 57
{
return false;
}
if (str[i] > 57 && str[i] < 65) // Capitals are 65 -> 90
{
return false;
}
if (str[i] > 90 && str[i] < 97) // Lowers are 97 -> 122
{
return false;
}
if (str[i] > 122)
{
return false;
}
}
return true;
}