View RSS Feed

jmurrayhead

JmhCommonControl - Providing Common Controls with Validation

Rate this Entry
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.
Example
Code:
<jmh:NameEntry ID="namFirstName" runat="server"
     FieldFormatErrorMessage="First Name contains invalid characters"
     IsRequired="true"
     RequiredFieldErrorMessage="First Name is required" />
Examples of Valid Input
John
John Doe
John O'Mally
Sarah Parker-Smith

PhoneNumber
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 Properties
  • 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.
Example
Code:
<jmh:PhoneNumber ID="phnHomePhone" runat="server"
     FieldFormatErrorMessage="Home Phone is not a valid phone number"
     IsRequired="false" />
Examples of Valid Input
(425) 555-0123
425-555-0123
425 555 0123
1-425-555-0123

SocialSecurityNumber
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 Properties
  • 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.
Example
Code:
<jmh:SocialSecurityNumber ID="ssnSocialSecurityNumber" runat="server"
     FieldFormatErrorMessage="Social Security Number contains invalid characters"
     IsRequired="true"
     RequiredFieldErrorMessage="Social Security Number is required"
     SsnFormat="Dashed" />
Examples of Valid Input
123-45-6789
123456789
URL
If you require users to enter URLs, the Url control will allow you to validate that the URLs are properly formatted.
Url Properties
  • 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.
Example
Code:
<jmh:Url ID="urlHomepage" runat="server"
     FieldFormatErrorMessage="Homepage is not a valid URL"
     IsRequired="true"
     Length="100"
     RequiredFieldErrorMessage="Homepage is required" />
Examples of Valid Input
developerbarn.com
http://www.developerbarn.com
https://mail.google.com
ftp.developerbarn.com

ZipCode
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 Properties
  • 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
Code:
<jmh:ZipCode ID="zipCode" runat="server"
     FieldFormatErrorMessage="Zip Code is not a valid Zip Code"
     RequiredFieldErrorMessage="Zip Code is required"
     IsRequired="true"
     ZipCodeFormat="Either" />
Example of Valid Input
55555
55555-5555

Email
Validates email address entry per the official standard RFC 2822
Email Properties
  • 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
Code:
<jmh:Email ID="emailAddress" runat="server"
     FieldFormatErrorMessage="Invalid Email Address"
     RequiredFieldErrorMessage="Email is required"
     IsRequired="true"
/>
Example of Valid Input
someone@somewhere.com
someone@government.gov
CustomTextBox
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 Properties
  • 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.
Example
Code:
<jmh:CustomTextBox ID="cusTextBox" runat="server"
     FieldFormatErrorMessage="Custom Field contains invalid characters"
     IsRequired="true"
     RequiredFieldErrorMessage="Custom Field is required"
ValidationExpression="^\d{11}$"
      />
Examples of Valid Input
Depends on your regular expression

Setup
  • 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 page
Code:
<%@ Register Assembly="JmhCommonControl" Namespace="JmhCommonControl" TagPrefix="jmh" %>
Register in Web.config
Code:
<pages>
  <controls>
    <add tagPrefix="jmh" namespace="JmhCommonControl" assembly="JmhCommonControl"/>
  </controls>
 </pages>
Latest Version Available: 1.0.0 Beta 2
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" ...)
Attached Thumbnails Attached Files

Submit "JmhCommonControl - Providing Common Controls with Validation" to Digg Submit "JmhCommonControl - Providing Common Controls with Validation" to del.icio.us Submit "JmhCommonControl - Providing Common Controls with Validation" to StumbleUpon Submit "JmhCommonControl - Providing Common Controls with Validation" to Google

Comments

  1. richyrich -
    richyrich's Avatar
    Nice work J.
    • |
    • permalink

SEO by vBSEO