Click here to Skip to main content
16,022,752 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
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
Posted

Try this simple LINQ

C#
bool isInvalid = charData.AsEnumerable().Any(chr => strToValidate.Contains(chr.Field<char>("invalidChar")));

</char>


In this charData is the table of invlid characters and strToValidate is a sting to validate

Hope this will help you.
 
Share this answer
 
C#
private bool invalidUserName(string UserName)
        {
            var dt = new DataTable();
            var isvalid = from DataRow i in dt.Rows where UserName.Contains(i["invalidchars"].ToString()) select false;
            return !isvalid.Contains(false);

        }
        private bool invalidUserName(string UserName)
        {
            var dt = new DataTable();
            IEnumerable<bool> isvalid;
            isvalid =
                dt.Rows.Cast<DataRow>().Where(i => UserName.Contains(i["invalidchars"].ToString())).Select(i => false);
            return !isvalid.Contains(false);

        }


[edit]code block language corrected[/edit]
 
Share this answer
 
v2
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;
   }


Also pass datatable dt to this method
 
Share this answer
 
v3
Comments
Greysontyrus 21-Nov-12 8:56am    
ManojDhobale: "I am looking for LINQ sort of code, instead of for/foreach loops" was in in the question but your solution is sound ^_^
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 ;)


/Edit: This assumes you use .Net4.0 and above
C#
char[] invalidCharacters = @"!@'#,.$%_/\".ToArray();
string textToTest = "foo@bar";

bool isValid = textToTest.ToArray()
               .Join(invalidCharacters,
                     ttt=>ttt,
                     ic=>ic,
                     (ttt,ic)=>ttt)
               .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 ^_^
 
Share this answer
 
v3
Comments
[no name] 21-Nov-12 23:48pm    
Thank you , later I converted those rows into char array and used something like:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900