A simple regex to see if a string is in a valid email format
public static bool IsValidEmail(string email)
{
string expression = @"^(([^<>()[\]\\.,;:\s@\""]+"
+ @"(\.[^<>()[\]\\.,;:\s@\""]+)*)|(\"".+\""))@"
+ @"((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}"
+ @"\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+"
+ @"[a-zA-Z]{2,}))$";
Regex re = new Regex(expression);
if (re.IsMatch(email))
return (true);
else
return (false);
<a href="http://www.fruitbatscode.com/net/c/uad4-is-valid-email#more-426" class="more-link">Read more...</a>
Converts an enum to a ListItemCollection so users can select an option from an enum. Just cast it back like so:
(enumToUse)Enum.Parse(typeof(enumToUse), DropDownList.SelectedItem.Value)
/// <summary>
/// Converts enum to ListItemCollection
/// </summary>
/// <param name="EnumToUse">Type of enum to use</param>
/// <param name="ListItems">Target LIstItemCollection</param>
public static void EnumToListItemCollection(System.Type EnumToUse, ListItemCollection ListItems)
{
string[] Names;
System.Array Values;
Int32 index;
<a href="http://www.fruitbatscode.com/net/c/uad3-enum-to-listitemcollection#more-421" class="more-link">Read more...</a>
Server controls are great but sometimes you need to change labels for different users, like change ‘Product Name’ to ‘Book Title’ when all they sell is books. You can create a load of properties or just use a simple util like this:
/// <summary>
/// Loops through labels in a control swapping matching values
/// </summary>
/// <param name="items">Dictionary where Key is the value to Search for and Value is wht to replace it with</param>
public static void LabelChange(ControlCollection controls, Dictionary<String, String> items)
{
foreach (Control c in controls)
{
if (c is Label)
{
Label lbl = c as Label;
if (items.ContainsKey(lbl.Text))
{
lbl.Text = items[lbl.Text];
}
}
}
}
I have a little plan to release a small code snippet a day till I run out or get bored
Just simple little utility functions that do things like check phone numbers, credit cards etc. Nothing special but just handy to have a list around!
Heres the first:
Does the string contain Html? Read more…
When creating type classes it can be useful to have an .Empty property so you know if an item has been populated. Its quite easy to achieve. All we do is create an instance of the class as a static property. Then we compare our instance to this one when we need to check.
Read more…
I always seem to forget how to do this so I’m just leaving a note for myself
To convert to string:
Jayrock.Json.Conversion.JsonConvert.ExportToString([object]);
To convert back again
([object])Jayrock.Json.Conversion.JsonConvert.Import(typeof([object]), result);