使用DataTable列值验证名称 [英] Validate Name using DataTable Column Values
问题描述
我有一个表,该表的列名为"InvalidCharacters",该表中约有100行,也就是说100个无效字符,我必须验证字符串[用户名],
针对这100个字符,如果字符串中的任何字符与表中的无效字符匹配,则认为该字符串无效.
我正在寻找 LINQ 这样的代码,而不是for/foreach循环
我应该在服务器端 进行验证,请在没有客户端脚本的情况下
感谢任何建议.
I have a table with a column named ''InvalidCharacters'', around 100 rows in the table,in the sense 100 invalid characters,I have to validate a string[username],
against these 100 characters, if any of the character in the string matches the invalid characters in the table, the string is considered to be invalid.
I am looking for LINQ sort of code, instead of for/foreach loops
I am supposed to perform validation on server side, no client side script please
any suggestion is appreciated
推荐答案
不确定为什么需要100行.您可以使用1个字符串来将其转换为代码中的char数组,但是我确定您必须有一个理由;)
/Edit:这假定您使用的是.Net4.0及更高版本
Not sure why you need to have 100 rows. 1 string will do as you can convert that to an array of chars in the code, but I''m sure you must have a reason ;)
/ This assumes you use .Net4.0 and above
char[] invalidCharacters = @"!@'#,.
%_/\" .ToArray(); 字符串 textToTest = "
%_/\".ToArray(); string textToTest = "foo@bar"; bool isValid = textToTest.ToArray() .Join(invalidCharacters, ttt=>ttt, ic=>ic, (ttt,ic)=>ttt) .Any();
我使用了无效的char字符串,但希望您能看到如何使用表.您可以使用此查询返回还使用的无效字符的列表,只需关闭Any().
通过评论让我知道是否需要此^ _ ^
I have used an invalid char string but I hope you can see how you could use your table instead. You could use this query to return a list of the invalid chars used also, just knock off the Any().
Let me know via comment if you need any more on this ^_^
我认为这对您有所帮助
I think this will help u
public bool IsValidString(string stringToCheck)
{
bool IsValidString = true;
foreach (DataRow row in dt.Rows)
{
string currentInvalidString = row["InvalidCharacters"].ToString();
if (currentInvalidString.Contains(stringToCheck))
{
IsValidString = false;
}
}
return IsValidString;
}
还将datatable dt传递给此方法
Also pass datatable dt to this method
这篇关于使用DataTable列值验证名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!