View Single Post
  #7 (permalink)  
Old January 6th, 2012, 01:17 PM
jpjamie jpjamie is offline
Authorized User
Points: 313, Level: 6
Points: 313, Level: 6 Points: 313, Level: 6 Points: 313, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2011
Posts: 74
Thanks: 21
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by Mike Brind View Post
Or, if all your controls start with or end with the same string in their name, you can use string.StartsWith or string.EndsWith in a foreach loop

Code:
foreach(string item in Request.Form){
    if(item.EndsWith("Comments")){
        if(Request[item].Length > 200){
            ModelState.AddError(item, "Less than 200 characters, please");
        }
    }
}
Thanks! Will try this out. All of my question's comment TextAreas are named Q1Comments, Q2Comments, Q3Comments etc., so they "EndsWith" "Comments" so will try this! Will be interesting to see if the code "splits off" the "Q1", "Q2", "Q3" etc. and runs the logic.

Last edited by jpjamie; January 6th, 2012 at 01:20 PM..
Reply With Quote