Click here to Skip to main content
12,449,910 members (45,885 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET LINQ
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 20-Nov-12 22:28pm
PBGuy1.8K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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 Wink | ;)


/Edit: This assumes you use .Net4.0 and above
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 ^_^
  Permalink  
v3
Comments
shabari7 21-Nov-12 23:48pm
   
Thank you , later I converted those rows into char array and used something like:
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
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 ^_^
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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]
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Try this simple LINQ

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.
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160826.1 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100