<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
<title>Snipplr</title>
<link>http://snipplr.com/language/c-sharp/tags/validation</link>
<description>Recent snippets posted on Snipplr.com</description>
<language>en-us</language>
<pubDate>Wed, 22 May 2013 23:52:00 GMT</pubDate>
<item>
<title>(C#) Check if a file is an executable. - jprochazka</title>
<link>http://snipplr.com/view/71221/check-if-a-file-is-an-executable/</link>
<description><![CDATA[ <p>A function to check the first two bytes to see if they match "MZ" marking it as an executable.</p> ]]></description>
<pubDate>Sat, 18 May 2013 05:02:45 GMT</pubDate>
<guid>http://snipplr.com/view/71221/check-if-a-file-is-an-executable/</guid>
</item>
<item>
<title>(C#) additonal fluent validation extension method for working with collection - marktaichen</title>
<link>http://snipplr.com/view/49723/additonal-fluent-validation-extension-method-for-working-with-collection/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 26 Feb 2011 09:58:39 GMT</pubDate>
<guid>http://snipplr.com/view/49723/additonal-fluent-validation-extension-method-for-working-with-collection/</guid>
</item>
<item>
<title>(C#) Fluent Validation extension method - marktaichen</title>
<link>http://snipplr.com/view/49721/fluent-validation-extension-method/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Sat, 26 Feb 2011 08:48:57 GMT</pubDate>
<guid>http://snipplr.com/view/49721/fluent-validation-extension-method/</guid>
</item>
<item>
<title>(C#) USPS Address Correction - RhinoX64</title>
<link>http://snipplr.com/view/46219/usps-address-correction/</link>
<description><![CDATA[ <p>requires you to register with USPS.
method requires your USPS API key.</p> ]]></description>
<pubDate>Wed, 29 Dec 2010 08:48:10 GMT</pubDate>
<guid>http://snipplr.com/view/46219/usps-address-correction/</guid>
</item>
<item>
<title>(C#) Validate phone liberally - housecor</title>
<link>http://snipplr.com/view/40152/validate-phone-liberally/</link>
<description><![CDATA[ <p>A very liberal validation of phone number. Assures there are at least 10 digits in there somewhere.</p> ]]></description>
<pubDate>Wed, 08 Sep 2010 23:39:14 GMT</pubDate>
<guid>http://snipplr.com/view/40152/validate-phone-liberally/</guid>
</item>
<item>
<title>(C#) Assure a valid email that .Net can actually send an email to is passed. - housecor</title>
<link>http://snipplr.com/view/40151/assure-a-valid-email-that-net-can-actually-send-an-email-to-is-passed/</link>
<description><![CDATA[ <p></p> ]]></description>
<pubDate>Wed, 08 Sep 2010 23:36:45 GMT</pubDate>
<guid>http://snipplr.com/view/40151/assure-a-valid-email-that-net-can-actually-send-an-email-to-is-passed/</guid>
</item>
<item>
<title>(C#) Visual Studio .NET Controls for Taking IP Address and MAC Address Info as Inputs - pckujawa</title>
<link>http://snipplr.com/view/22421/visual-studio-net-controls-for-taking-ip-address-and-mac-address-info-as-inputs/</link>
<description><![CDATA[ <p>This snippet builds on my last snippet regarding [User input validation in Visual Studio .NET using MaskedTextBoxes and an ErrorProvider](http://snipplr.com/view/22419/user-input-validation-in-visual-studio-net-using-maskedtextboxes-and-an-errorprovider/).

Using a MaskedTextBox (MTB) works well for a MAC address because the length of the input is always the same: six octets (a.k.a. bytes) with hexadecimal input (0-9A-F) with or without colon (:) or dash (-) separators. You can set the Mask to be ">aa\:aa\:aa\:aa\:aa\:aa", which will make all characters typed in go to uppercase ('>'), accept, optionally, two alphanumeric characters ('aa') followed by a literal colon ('\:'), and so on. (The reason for the literal colon is so that it isn't translated to a different Time separator character.)

Unfortunately, I don't think there is a way to specify that the separator can be either a dash *or* a colon, but, in my test, pasting in input with either separator (or none) works ok (i.e. "A0-b1-C2-d3-E4-f5", "A0:b1:C2:d3:E4:f5", or "A0b1C2d3E4f5"). Another limitation is that the input is optional, but that can be compensated for in your own validation code. That code should start by assigning the ValidatingType of your MTB to typeof(Mac). (Of course, you need to create a Mac class for doing MAC address manipulation, since .NET doesn't include one for you.) [Edit: .NET does have a built-in MAC address class: System.Net.NetworkInformation.PhysicalAddress] Look up the MSDN for ValidatingType to know what all the class needs to support for this functionality.

For the IP address, things are a little easier but a little worse. The MTB doesn't appear to support variable-length input (e.g. "1" as well as "192"), so we can't use the mask to our advantage. (If you don't believe me, try a Mask of "990\.990\.990\.990" and see.) So our UI isn't as nice, but oh well. For validation, set the ValidatingType to typeof(System.Net.IPAddress) and cast your e.ReturnValue in your TypeValidationCompleted event handler to that same type.

That's it. A bit of a cursory explanation, but you should be able to piece together the rest. Leave questions if you have 'em.</p> ]]></description>
<pubDate>Tue, 03 Nov 2009 15:33:13 GMT</pubDate>
<guid>http://snipplr.com/view/22421/visual-studio-net-controls-for-taking-ip-address-and-mac-address-info-as-inputs/</guid>
</item>
<item>
<title>(C#) User input validation in Visual Studio .NET using MaskedTextBoxes and an ErrorProvider - pckujawa</title>
<link>http://snipplr.com/view/22419/user-input-validation-in-visual-studio-net-using-maskedtextboxes-and-an-errorprovider/</link>
<description><![CDATA[ <p>I just discovered that the MaskedTextBox control allows you to set the type of data which should be entered in the control and helps you do the validation of that data when necessary. The trick is to set the ValidatingType of the MaskedTextBox control to the type of data you want to get from the user. If you do this, as well as subscribe to the TypeValidationCompleted event, on the form Load event, you can handle user validation inside your TypeValidationCompleted event handler.

If you want to read about the MaskedTextBox, it's all on MSDN. What you should be aware of, though, is that the Mask is *not* required. When the mask is blank, you can accept input just like you can with a TextBox.

The ErrorProvider is a WinForms control that allows you to show a red exclamation mark with an error message tooltip next to a control. It's really useful when combined with the above data validation method.

The example shows us setting the type of data which should be entered into the text box and then subscribing to the corresponding event. In that event handler, we check if the input is valid (don't know what all that does, but it works - play with it) and, if so, cast the value to the correct data type in order to use for some purpose. We also clear any errors in the ErrorProvider (not sure of the best way to use this control, but this works). If the input is not valid, we set the error shown by the ErrorProvider (the e.Message is generally the same as the Message parameter of any exception that has been thrown). (You can set the error to the control by name, or generically by using the sender argument.)</p> ]]></description>
<pubDate>Tue, 03 Nov 2009 15:05:27 GMT</pubDate>
<guid>http://snipplr.com/view/22419/user-input-validation-in-visual-studio-net-using-maskedtextboxes-and-an-errorprovider/</guid>
</item>
<item>
<title>(C#) Validate string via regular expression - leonbda</title>
<link>http://snipplr.com/view/11222/validate-string-via-regular-expression/</link>
<description><![CDATA[ <p>Validate any string programmatically via regular expression.</p> ]]></description>
<pubDate>Thu, 15 Jan 2009 15:39:35 GMT</pubDate>
<guid>http://snipplr.com/view/11222/validate-string-via-regular-expression/</guid>
</item>
</channel>
</rss>