JmhCommonControl - Providing Common Controls with Validation
by
on March 10th, 2010 at 01:41 PM (262 Views)
If you're like me, you don't like rewriting/copy & pasting validation code for controls that we use all the time. This enticed me to build a collection of these common controls that I can reuse in every application and save myself from a lot of typing. Thus the birth of JmhCommonControl.
Currently, the JmhCommonControl assembly consists of 6 controls: NameEntry, PhoneNumber, SocialSecurity, Url, ZipCode, and CustomTextBox. Continue reading for information on each of these.
NameEntry
Most forms I build require the entry of a person's first, last and sometimes middle name or initial. The NameEntry control can be used for each of these fields.NameEntry Properties
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid name (i.e. J@s0n).
- Length - This is the maximum amount of characters a user can enter. If left blank, the default is 40.
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
ExamplePhoneNumber
Examples of Valid InputCode:<jmh:NameEntry ID="namFirstName" runat="server" FieldFormatErrorMessage="First Name contains invalid characters" IsRequired="true" RequiredFieldErrorMessage="First Name is required" />
John
John Doe
John O'Mally
Sarah Parker-Smith
This is another very common form element. The PhoneNumber control can be used when you want users to enter a valid US phone number.PhoneNumber PropertiesSocialSecurityNumber
Example
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid phone number (i.e. abc-def-8765).
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Examples of Valid InputCode:<jmh:PhoneNumber ID="phnHomePhone" runat="server" FieldFormatErrorMessage="Home Phone is not a valid phone number" IsRequired="false" />
(425) 555-0123
425-555-0123
425 555 0123
1-425-555-0123
Perhaps not as common as most of these, but if you develop intranet applications for any corporation, government or military entity, this will likely be used a lot. The SocialSecurityNumber control allows you to choose between dashed and non-dashed social security numbers (i.e. 123-45-6789, 123456789).SocialSecurityNumber PropertiesURL
Example
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid social security number (i.e. abcdefghi).
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- SsnFormat - Allows you to choose between dashed or non-dashed validation.
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Examples of Valid InputCode:<jmh:SocialSecurityNumber ID="ssnSocialSecurityNumber" runat="server" FieldFormatErrorMessage="Social Security Number contains invalid characters" IsRequired="true" RequiredFieldErrorMessage="Social Security Number is required" SsnFormat="Dashed" />
123-45-6789
123456789
If you require users to enter URLs, the Url control will allow you to validate that the URLs are properly formatted.Url PropertiesZipCodeExample
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid URL (i.e. john.).
- Length - This is the maximum amount of characters a user can enter. If left blank, the default is 40.
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Examples of Valid InputCode:<jmh:Url ID="urlHomepage" runat="server" FieldFormatErrorMessage="Homepage is not a valid URL" IsRequired="true" Length="100" RequiredFieldErrorMessage="Homepage is required" />
developerbarn.com
http://www.developerbarn.com
https://mail.google.com
ftp.developerbarn.com
A quick solution to address-entry forms, the ZipCode control allows you to accept zip codes in either 5-digit, 9-digit, or either formats.ZipCode PropertiesEmail
Example
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid zip code (i.e. #$ghygaas).
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- ZipCodeFormat - Allows you to choose between 5-digit, 9-digit or either formats.
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Example of Valid InputCode:<jmh:ZipCode ID="zipCode" runat="server" FieldFormatErrorMessage="Zip Code is not a valid Zip Code" RequiredFieldErrorMessage="Zip Code is required" IsRequired="true" ZipCodeFormat="Either" />
55555
55555-5555
Validates email address entry per the official standard RFC 2822Email PropertiesCustomTextBox
Example
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid email address (i.e. helpme.com, me@local).
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- Text - Gets or sets the value in the textbox.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Example of Valid InputCode:<jmh:Email ID="emailAddress" runat="server" FieldFormatErrorMessage="Invalid Email Address" RequiredFieldErrorMessage="Email is required" IsRequired="true" />
someone@somewhere.com
someone@government.gov
If the other five controls in the JmhCommonControl library don't meet your needs the CustomTextBox control is your friend. This allows you to use your own regular expression just as if you were using the RegularExpressionValidator control, only easier! Also, if you leave the ValidationExpression field blank, it will act as a textbox with just a RequiredFieldValidator applied (if IsRequired is set to true).CustomTextBox PropertiesSetup
Example
- FieldFormatErrorMessage - This is the error message you want to show to the user when they enter an invalid input.
- IsRequired - Specifies whether the user must enter a value or not
- RequiredFieldErrorMessage - This is the error message you want to show to the user when they leave the field blank (IsRequired must be set to true).
- Text - Gets or sets the value in the textbox.
- ValidationExpression - This is your own custom validation expression, just like you would use in the RegularExpressionValidator control.
- ValidationGroup - Like the regular ASP.Net validation controls, this allows you to specify which ValidationGroup the control should be validated with.
Examples of Valid InputCode:<jmh:CustomTextBox ID="cusTextBox" runat="server" FieldFormatErrorMessage="Custom Field contains invalid characters" IsRequired="true" RequiredFieldErrorMessage="Custom Field is required" ValidationExpression="^\d{11}$" />
Depends on your regular expression
- To use the JmhCommonControl library: Download the zip file and extract JmhCommonControl.dll to an area on your hard disk.
- Copy JmhCommonControl library to your application's bin folder OR Right-Click the bin folder in Visual Studio and click "Add Reference" and choose JmhCommonControl.dll from the dialogue.
- Register the assembly in either the pages you are going to use it on OR in the web.config file (see below).
Register in the pageLatest Version Available: 1.0.0 Beta 2
Register in Web.configCode:<%@ Register Assembly="JmhCommonControl" Namespace="JmhCommonControl" TagPrefix="jmh" %>
Code:<pages> <controls> <add tagPrefix="jmh" namespace="JmhCommonControl" assembly="JmhCommonControl"/> </controls> </pages>
Release Notes
v1.0.0 Beta 1 : 10-Mar-2010 : Initial release.
v1.0.0 Beta 2 : 24-Mar-2010 : Added Email control. Added ability to set TextMode of SocialSecurityNumber control. Fixed bug when explicitly setting the Text property of any controls (i.e. <jmh:ZipCode ID="zipCode" runat="server" Text="12345" ...)








Email Blog Entry