SVCs are asp.net controls that have validation built into the control that is set through property values. The controls that have been overridden to contain built-in validation are CheckBoxList, DropDownList, ListBox, RadioButtonList and TextBox.
Current production version is 1.0.0.0, and is available for checkout from: https://svn.asu.edu/svn/webcommune/.net/asu.webutilities.selfvalidatingcontrols/tags/1.0.0.0/
If you wish to expand on the SelfValidatingControls please check it out from: https://svn.asu.edu/svn/webcommune/.net/asu.webutilities.selfvalidatingcontrols/trunk/
You must be a member of the ASU Web Developer Commune group to get access to this project.
If you want this assembly to appear in the Add Reference dialog box, simply place a copy of the assembly in the following folder C:\Program Files\Microsoft Visual Studio 8\Common7\IDE\PublicAssemblies.Â
Some examples of validation types include required, date, credit card number, phone number and email address.
What happens when a control is not valid:
On every postback, a self-validating control will run its validation routine if the control is visible. If the control is not valid, the following occurs:
IsValid property will be set to false
ErrorMessage property will contain a message stating why the control is invalid and a css class style called 'invalidEntry' will be applied to the control. This will allow you to make the control stand out to the user (such as setting the background color to yellow).
ToolTip property is updated to contain the error message.
How to determine if a page containing self-validating controls is valid:
To determine whether the asp.net page containing the self-validating control(s) is valid, simply check the Page.IsValid property. If the value is true, all validation checks passed. If the value is false, you can obtain the reasons for the validation error(s) in one of the following ways:
If you have an asp.net ValidationSummary control on the page, it will automatically be populated with the error messages. Otherwise, you can loop through the Page.Validatiors collection to get the error message in a manner like the following:
StringBuilder sb = new StringBuilder();
sb.Append("<ul>");
foreach(IValidator v in Page.Validators)
{
  if (== v.IsValid)
  {
    sb.Append("<li>");
    sb.Append(v.ErrorMessage);
    sb.Append("</li>");
  }
}
sb.Append("</ul>");
Detailed documentation
CheckBoxList Control
DropDownList Control
ListBox Control
RadioButtonList Control
TextBox Control
How to add a reference to the SVC controls for a VS.NET project
Why? This reference will allow you to access the SVC properties and methods through your C# code. In the project that you want to use the self validating controls, add a Reference to ASU.WebUtilities.SelfValidatingControls. To do this add the following lines to the system.web section of the web.config file for the project
<pages>
  <controls>
    <add tagPrefix="svc" namespace="ASU.WebUtilities.SelfValidatingControls" assembly="ASU.WebUtilities.SelfValidatingControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bef2d522ac0c11e"/>
  </controls>
</pages>
How to add a reference to the SVC controls in an .aspx or .ascx page
Add the following line of code to the page code:
<%@ Register TagPrefix="svc" NameSpace="ASU.WebUtilities.SelfValidatingControls" Assembly="ASU.WebUtilities.SelfValidatingControls, Version=1.0.0.0, Culture=neutral, PublicKeyToken=0bef2d522ac0c11e"%>
To use a SVC textbox, the code will look something like the following:
<svc:textbox id="_tbxAsuID" runat="server" name="ASU ID" type="asuID" required="true" tabindex="1" tooltip="Enter the student's ASU ID"/>
The main difference is that instead of asp:TextBox, you are using svc:TextBox. The 'svc' tag prefix is defined in the '@ Register tag in the last section. You can change the 'svc' tag prefix to any that you desire.
Disclaimer
The foundation for the textbox validation expressions came from 'Hive ]]Multi-Validating[[ Self Textbox Control' on codeproject.com. I converted this project to C#, increased the textbox validation and added validation for check box lists, drop down lists, list boxes and radio button lists.
CheckBoxList
The only properties shown are those that differ from the standard CheckBoxList, all the standard properties function as they always have.
Properties
- ErrorMessage - when the control is invalid, this property contains a the reason for it being invalid. Only the first error encountered will be shown.
- IsValid - the control will set this value on postback. The value can also be programmatically set.
- MaxSelection - indicates the maximum number of selections the control must have. Default value = 0, which indicates that there is no maximum selection requirement.
- MinSelection - indicates the minimum number of selections the control must have. Default value = 0, which indicates that there is no minimum selection requirement.
- Name - this is the name of the control to show at the beginning of the error message. For example if Name="Credit Card Number", the error message will be written as "Credit Card Number is ...". If a name is not given, the ID of the control will be displayed at the beginning of the error message.
- Required - indicates if the control must have at least one value checked. Default value = false.
- SelectedValue - This property is overridden in order to programmatically select the default value for a state list. Since the state list is not created until pre-render, any selected value set in !PostBack or an event handler would not properly set the value unless this method was overridden.
- ShowStateNames - when Type = StateCode, this property indicates whether the text value of the check boxes will be populated with the state names or the 2-character state abbreviations.
- Type - indicates a specific type of validation that is to take place on postback. There is only one option of StateCode at this time.
- StateCode - This validation type serves two purposes:
- loads the control with the list of states during pre-render. The list of states were obtained from the US Post Office
- on postback, checks that each selected value is a proper 2-character state abbreviation contained in the built-in list of states.
Methods
RemoveValidation
Use this method to remove validation requirements for a control. Now you may be wondering why you would even need this if you are using validating controls. Well, you may run into instances when you have one page that has different sections that get submitted at different times. Every time the page is submitted (CausesValidation=true), the self-validating controls will always perform their validation checks and a section that is not being used could come back as being invalid and cause problems.
When this method is called, the control is removed from Page.Validators resulting in the call to Page.IsValid to only be checking the controls that you are about for that button click/command event. Below is an example of removing controls that do not pertain to the button click event.
// remove the correspondence record validators since
// they do not need to be valid for this postback
_tbxDate.RemoveValidation();
_ddlType.RemoveValidation();
_ddlReason.RemoveValidation();
_tbxOtherReason.RemoveValidation();if (Page.IsValid)
{
  ...
}
On the next postback, the controls whose RemoveValidation method was called will be automatically added back into the Page.Validators collection.
AddValidation
Use this method to add a control to the Page.Validators collection. This method should really only be called if RemoveValidation was called and then the control needs to be validated within the same postback. After calling this method, you will need to call Page.Validate() to get the validation status of the control.
DropDownList
The only properties shown are those that differ from the standard DropDownList, all the standard properties function as they always have.
Properties
- CanBeFirstItem - the first entry in a drop down list is selected by default. In some drop down lists, the first entry is usually a blank item or an item stating 'select one of the following' and, therefore, should not be allowed to be selected. If this first entry is ok to select, set the property to true. The default value for this property is false.
- Compare - you can compare the selected value of a drop down list with the selected value of another self-validating drop down list control. To indicate which drop down list control you wish to compare against, you must set the CompareID property (see below). The possible comparison values for this property are:
- Equal - checks to see if the two selected values are equal
- NotEqual - checks to see if the two selected values are not equal
- LessThan - checks to see if this drop down list's selected value is less than the other drop down lists selected value
- LessThanOrEqual - checks to see if this drop down list's selected value is less than or equal to the other drop down lists selected value
- GreaterThan - checks to see if this drop down list's selected value is greater than the other drop down lists selected value
- GreaterThanOrEqual - checks to see if this drop down list's selected value is greater than or equal to the other drop down lists selected value
- CompareID - The ID of a self-validating drop down list control you wish to compare against this drop down list control.
- ErrorMessage - when the control is invalid, this property contains a the reason for it being invalid. Only the firs error encounter will be shown.
- IsValid - the control will set this value on postback. The value can also be programmatically set.
- Name - this is the name of the control to show at the beginning of the error message. For example if Name="Credit Card Number", the error message will be written as "Credit Card Number is ...". If a name is not given, the ID of the control will be displayed at the beginning of the error message.
- Required - indicates if the control must have at least one value checked. Default value = false.
- SelectedValue - This property is overridden in order to programmatically select the default value for a state list. Since the state list is not created until pre-render, any selected value set in == PostBack or an event handler would not properly set the value unless this method was overridden.
- ShowStateNames - when Type = StateCode, this property indicates whether the text value of the check boxes will be populated with the state names or the 2-character state abbreviations.
- Type - indicates a specific type of validation that is to take place on postback. There is only one option of StateCode at this time.
- StateCode - This validation type serves two purposes:
- loads the control with the list of states during pre-render. The list of states were obtained from the US Post Office
- on postback, checks that each selected value is a proper 2-character state abbreviation contained in the built-in list of states.
Methods
RemoveValidation
Use this method to remove validation requirements for a control. Now you may be wondering why you would even need this if you are using validating controls. Well, you may run into instances when you have one page that has different sections that get submitted at different times. Every time the page is submitted (CausesValidation=true), the self-validating controls will always perform their validation checks and a section that is not being used could come back as being invalid and cause problems.
When this method is called, the control is removed from Page.Validators resulting in the call to Page.IsValid to only be checking the controls that you are about for that button click/command event. Below is an example of removing controls that do not pertain to the button click event.
// remove the correspondence record validators since
// they do not need to be valid for this postback
_tbxDate.RemoveValidation();
_ddlType.RemoveValidation();
_ddlReason.RemoveValidation();
_tbxOtherReason.RemoveValidation();if (Page.IsValid)
{
  ...
}
On the next postback, the controls whose RemoveValidation method was called will be automatically added back into the Page.Validators collection.
AddValidation
Use this method to add a control to the Page.Validators collection. This method should really only be called if RemoveValidation was called and then the control needs to be validated within the same postback. After calling this method, you will need to call Page.Validate() to get the validation status of the control.
ListBox
The only properties shown are those that differ from the standard ListBox, all the standard properties function as they always have.
Properties
- ErrorMessage - when the control is invalid, this property contains a the reason for it being invalid. Only the firs error encounter will be shown.
- IsValid - the control will set this value on postback. The value can also be programmatically set.
- MaxSelection - indicates the maximum number of selections the control must have. Default value = 0, which indicates that there is no maximum selection requirement.
- MinSelection - indicates the minimum number of selections the control must have. Default value = 0, which indicates that there is no minimum selection requirement.
- Name - this is the name of the control to show at the beginning of the error message. For example if Name="Credit Card Number", the error message will be written as "Credit Card Number is ...". If a name is not given, the ID of the control will be displayed at the beginning of the error message.
- Required - indicates if the control must have at least one value checked. Default value = false.
- SelectedValue - This property is overridden in order to programmatically select the default value for a state list. Since the state list is not created until pre-render, any selected value set in !PostBack or an event handler would not properly set the value unless this method was overridden.
- ShowStateNames - when Type = StateCode, this property indicates whether the text value of the check boxes will be populated with the state names or the 2-character state abbreviations.
- Type - indicates a specific type of validation that is to take place on postback. There is only one option of StateCode at this time.
- StateCode - This validation type serves two purposes:
- loads the control with the list of states during pre-render. The list of states were obtained from the US Post Office
- on postback, checks that each selected value is a proper 2-character state abbreviation contained in the built-in list of states.
Methods
RemoveValidation
Use this method to remove validation requirements for a control. Now you may be wondering why you would even need this if you are using validating controls. Well, you may run into instances when you have one page that has different sections that get submitted at different times. Every time the page is submitted (CausesValidation=true), the self-validating controls will always perform their validation checks and a section that is not being used could come back as being invalid and cause problems.
When this method is called, the control is removed from Page.Validators resulting in the call to Page.IsValid to only be checking the controls that you are about for that button click/command event. Below is an example of removing controls that do not pertain to the button click event.
// remove the correspondence record validators since
// they do not need to be valid for this postback
_tbxDate.RemoveValidation();
_ddlType.RemoveValidation();
_ddlReason.RemoveValidation();
_tbxOtherReason.RemoveValidation();if (Page.IsValid)
{
  ...
}
On the next postback, the controls whose RemoveValidation method was called will be automatically added back into the Page.Validators collection.
AddValidation
Use this method to add a control to the Page.Validators collection. This method should really only be called if RemoveValidation was called and then the control needs to be validated within the same postback. After calling this method, you will need to call Page.Validate() to get the validation status of the control.
RadioButtonList
The only properties shown are those that differ from the standard RadioButtonList, all the standard properties function as they always have.
Properties
- Compare - you can compare the selected value of a radio button list with the selected value of another self-validating radio button list control. To indicate which radio button list control you wish to compare against, you must set the CompareID property (see below). The possible comparison values for this property are:
- Equal - checks to see if the two selected values are equal
- NotEqual - checks to see if the two selected values are not equal
- LessThan - checks to see if this radio button list's selected value is less than the other radio button lists selected value
- LessThanOrEqual - checks to see if this radio button list's selected value is less than or equal to the other radio button lists selected value
- GreaterThan - checks to see if this radio button list's selected value is greater than the other radio button lists selected value
- GreaterThanOrEqual - checks to see if this radio button list's selected value is greater than or equal to the other radio button lists selected value
- CompareID - The ID of a self-validating radio button list control you wish to compare against this radio button list control.
- ErrorMessage - when the control is invalid, this property contains a the reason for it being invalid. Only the first error encounter will be shown.
- IsValid - the control will set this value on postback. The value can also be programmatically set.
- Name - this is the name of the control to show at the beginning of the error message. For example if Name="Credit Card Number", the error message will be written as "Credit Card Number is ...". If a name is not given, the ID of the control will be displayed at the beginning of the error message.
- Required - indicates if the control must have at least one value checked. Default value = false.
- SelectedValue - This property is overridden in order to programmatically select the default value for a state list. Since the state list is not created until pre-render, any selected value set in !PostBack or an event handler would not properly set the value unless this method was overridden.
- ShowStateNames - when Type = StateCode, this property indicates whether the text value of the check boxes will be populated with the state names or the 2-character state abbreviations.
- Type - indicates a specific type of validation that is to take place on postback. There is only one option of StateCode at this time.
- StateCode - This validation type serves two purposes:
- loads the control with the list of states during pre-render. The list of states were obtained from the US Post Office
- on postback, checks that each selected value is a proper 2-character state abbreviation contained in the built-in list of states.
Methods
RemoveValidation
Use this method to remove validation requirements for a control. Now you may be wondering why you would even need this if you are using validating controls. Well, you may run into instances when you have one page that has different sections that get submitted at different times. Every time the page is submitted (CausesValidation=true), the self-validating controls will always perform their validation checks and a section that is not being used could come back as being invalid and cause problems.
When this method is called, the control is removed from Page.Validators resulting in the call to Page.IsValid to only be checking the controls that you are about for that button click/command event. Below is an example of removing controls that do not pertain to the button click event.
// remove the correspondence record validators since
// they do not need to be valid for this postback
_tbxDate.RemoveValidation();
_ddlType.RemoveValidation();
_ddlReason.RemoveValidation();
_tbxOtherReason.RemoveValidation();if (Page.IsValid)
{
  ...
}
On the next postback, the controls whose RemoveValidation method was called will be automatically added back into the Page.Validators collection.
AddValidation
Use this method to add a control to the Page.Validators collection. This method should really only be called if RemoveValidation was called and then the control needs to be validated within the same postback. After calling this method, you will need to call Page.Validate() to get the validation status of the control.
TextBox
The only properties shown are those that differ from the standard TextBox, all the standard properties function as they always have.
Properties
- Chars - indicates that the textbox value must adhere to a certain character set. The possible values for this property are:
- All - allows all characters to be used.
- Alpha - allows all alpha characters to be used.
- AlphaNumeric - allows all alpha-numeric characters to be used.
- NoDoubleQuotes - allows all characters except double quotes.
- NONE - indicates that character restriction will not be checked when validating the textbox control (default value).
- NonHTML - allows all non-html characters to be used.
- NoQuotes - allows all characters except quotes to be used.
- Numeric - allows all numeric characters to be used.
- UserDefined - you supply your own regular expression to use to validate through the CustomChars property. If the value is not valid, the CustomCharsError will be shown.
- Compare - compare the value of a textbox with the value of another self-validating textbox control. To indicate which textbox control you wish to compare against, you must set the CompareID property. The possible comparison values for this property are:
- CaseEqual - checks to see if the two values are equal with the same casing.
- Equal - checks to see if the two values are equal (casing ignored).
- NotEqual - checks to see if the two values are not equal.
- LessThan - checks to see if this textbox's value is less than the other textbox's value.
- LessThanOrEqual - checks to see if this textbox's value is less than or equal to the other textbox's value.
- GreaterThan - checks to see if this textbox's value is greater than the other textbox's value.
- GreaterThanOrEqual - checks to see if this textbox's value is greater than or equal to the other textbox's value.
- CompareID - The ID of a self-validating radio button list control you wish to compare against this radio button list control.
- CreditCardType - When Type = CreditCard, this property is populated with the card the credit card numbers correspond to after the credit card has been processed. The possible values credit card types are:
- AMEX
- DinersClub
- Discover
- EnRoute
- JCB
- MasterCard
- Visa
- CustomChars - Regular Expression used to validate the textbox value when Chars = UserDefined.
- CustomCharsError - Error message that is to be displayed when the textbox value is invalid with Chars = UserDefined.
- CustomError - Error message that is to be displayed when the textbox value is invalid with Type = UserDefined.
- CustomRegExp - Regular Expression used to validate the textbox value when Type = UserDefined.
- ErrorMessage - when the control is invalid, this property contains a the reason for it being invalid. Only the first error encounter will be shown.
- IsValid - the control will set this value on postback. The value can also be programmatically set.
- MaximumValue - indicates that the textbox value must be less than or equal to the property value. If a MinimumValue is also set, the textbox value must be between the minimum and maximum value. This property is only processed during validation if the Type is one of the following values:
- AdvancedDate
- Currency
- Date
- Double
- Int
- MonthYear
- NONE - compares string values
- Percentage
- Year
- YearSemester
- MaxLength - indicates the maximum number of characters that can be entered into the textbox. If no value is supplied, this property is not processed during validation.
- MinimunValue - indicates that the textbox value must be greater than or equal to the property value. If a MaximumValue is also set, the textbox value must be between the minimum and maximum value. This property is only processed during validation if the Type is one of the following values:
- AdvancedDate
- Currency
- Date
- Double
- Int
- MonthYear
- NONE - compares string values
- Percentage
- Year
- YearSemester
- MinLength - indicates the minimum number of characters that can be entered into the textbox. If no value is supplied, this property is not processed during validation.
- Name - this is the name of the control to show at the beginning of the error message. For example if Name="Credit Card Number", the error message will be written as "Credit Card Number is ...". If a name is not given, the ID of the control will be displayed at the beginning of the error message.
- Required - indicates if the textbox is required to have a value. Default value = false.
- Type - indicates a specific type of validation that is to take place on postback. Possible values for this property are:
- AdvancedDate
- RegExp - ^(?:(?:(?:0?[13578]|1[02])(\/|-)31)|(?:(?:0?[1,3-9]|1[0-2])(\/|-)(?:29|30)))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:0?[1-9]|1[0-2])(\/|-)(?:0?[1-9]|1\d|2[0-8]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(0?2(\/|-)29)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$
- If pass the RegExp, the value is valid if it can be converted to a valid DateTime (e.g., DateTime.Parse(value))
- AffiliateID - any 10-digit number
- RegExp = \d{10}
- Has a default MaxLength = 10
- ASUID - Any 9-digit number with or without the dashes (e.g., 123-45-6789 or 123456789)
- RegExp = ^\d{3}-?\d{2}-?\d{4}$
- Has a default MaxLength = 11
- AsuriteID - an id that starts with 3 lower case letters and is followed by any combination of 2-5 lower case letters or numbers.
- RegExp = [a-z]{3}[a-z0-9]{2,5}
- Has a default MaxLength = 8
- CreditCard - the credit card number is validated using a set of equations.
- If the credit card number does not follow one of the following number formats, the credit card number will be invalid:
- AMEX - starts with 34 or 37 and has a length of 15
- DinersClub - starts with 300, 301, 302, 303, 304, 305, 36 or 38 and has a length of 14
- Discover - starts with 6011 and has a length of 16
- EnRoute - starts with 2014 or 2149 and has a length of 15
- JCB - starts with 3 and has a length of 16 or starts with 2131 or 1800 and has a length of 15
- MasterCard - starts with 51, 52, 53, 54 or 55 and has a length of 16
- Visa - starts with a 4 and has a length of 13 or 16
- Has a default MaxLength = 19
- If the credit card number does not follow one of the following number formats, the credit card number will be invalid:
- Currency - any decimal value with or without the ',' (e.g., 123,123,123.12 or 123123123.12)
- RegExp = ^\d{1,3}(,\d{3})*(\.\d\d)?$|^\d+(\.\d\d)?$
- Date - mm/dd/yyyy or mm-dd-yyyy. The month and day can be 1 or 2-digits but the year must be 4-digits. As long as the date is valid, it is accepted, so 2/5/1042 and 7/8/2245 are valid.
- RegExp = ^\d{1,2}/|-\d{1,2}/|-\d{4}$
- If pass the RegExp, the value is valid if it can be converted to a valid DateTime (e.g., DateTime.Parse(value))
- Has a default MaxLength = 10
- Double - any positive or negative number that can have decimal places.
- RegExp = ^\d+$|^\d+.\d+$
- Email - xxx@xxx.xxx, where x can ben any upper-case letter, lower-case letter or digit, '_', '-', or '.'.
- RegExp = ([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)
- GUID
- RegExp = [{][a-fA-F0-9]{8}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{4}[-][a-fA-F0-9]{12}[}]
- Has a default MaxLength = 38
- Int - any whole number that is positive or negative.
- RegExp = ^\d+$
- InternationalPhone - 7-20 digit number. No formatting is checked.
- RegExp = ^\d(\d|-){7,20}
- IPAddress
- RegExp = ^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$
- Has a default MaxLength = 15
- ISBN - the code to validate an ISBN number was adapted from isbntools.com.
- Has a default MaxLength = 16
- MonthYear - mm/yyyy or mm-yyyy, where the month can be 1 or 2-digits and the year is 4-digits >= 1900
- RegExp = ^\d{1,2}(/|-)(19[0-9][0-9]|[2-9][0-9][0-9][0-9])$
- If pass the RegExp, the value is valid if it can be converted to a valid DateTime (e.g., DateTime.Parse(value))
- Has a default MaxLength = 7
- NONE - treat the value as a string. This is the default value for Type
- Password - set of lower case and upper case letters with a length between MinLength and MaxLength
- RegExp = ^[a-zA-Z]\w{" + MinLength + "," + MaxLength + "}$
- Percentage - any decimal between 0 and 100. The presence of '%' will cause the value to be invalid
- RegExp = ^(0*100{1,1}\.?((?<=\.)0*)?$)|(^0*\d{0,2}\.?((?<=\.)\d*)?)$
- Has a default MaxLength = 6
- Phone - 10-digit phone number, area code is required (e.g., (555) 123-4567 or 555 123 4567).
- RegExp = ^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$
- Has a default MaxLength = 16
- SSN - Any 9-digit number with or without the dashes (e.g., 123-45-6789 or 123456789)
- RegExp - ^\d{3}-?\d{2}-?\d{4}$
- Has a default MaxLength = 11
- StateCode - verifies that the textbox value is a valid state code. The internal list of state codes was captured from usps.com.
- Utilities.IsInStateList(value)
- Has a default MaxLength = 2
- UKDate
- RegExp = ^(?:(?:0?[1-9]|1\d|2[0-8])(\/|-)(?:0?[1-9]|1[0-2]))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(?:(?:31(\/|-)(?:0?[13578]|1[02]))|(?:(?:29|30)(\/|-)(?:0?[1,3-9]|1[0-2])))(\/|-)(?:[1-9]\d\d\d|\d[1-9]\d\d|\d\d[1-9]\d|\d\d\d[1-9])$|^(29(\/|-)0?2)(\/|-)(?:(?:0[48]00|[13579][26]00|[2468][048]00)|(?:\d\d)?(?:0[48]|[2468][048]|[13579][26]))$
- If pass the RegExp, the value is valid if it can be converted to a valid DateTime (e.g., DateTime.Parse(value))
- UKPostalCode
- RegExp = ^(([A-Z]{1,2}\d{1,2})|([A-Z]{1,2}\d[A-Z]))\s?\d[ABDEFGHJLNPQRSTUWXYZ]{2}$
- URL
- RegExp = http://|https://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
- UserDefined - you supply your own regular expression to use to validate through the CustomRegExp property. If the value is not valid, the CustomError will be shown.
- Year - any 4-digit number >= 1900
- RegExp = (19[0-9][0-9]|[2-9][0-9][0-9][0-9])
- Has a default MaxLength = 4
- YearSemester - yyyys, where yyyy is a 4-digit year >= 1970 and s is the semester code of 1=spring; 3=1st summer session; 5=2nd summer session; 7=fall/winter.
- RegExp = (19[7-9][0-9]|[2-9][0-9][0-9][0-9])(1|3|5|7)
- Has a default MaxLength = 5
- ZipCode - either a 5-digit zip code or a 10-digit zip code is valid (e.g., 12345 or 12345-6789)
- RegExp = ^\s*\d{5}(?:[-]\d{4})?\s*$
- Has a default MaxLength = 10
Methods
RemoveValidation
Use this method to remove validation requirements for a control. Now you may be wondering why you would even need this if you are using validating controls. Well, you may run into instances when you have one page that has different sections that get submitted at different times. Every time the page is submitted (CausesValidation=true), the self-validating controls will always perform their validation checks and a section that is not being used could come back as being invalid and cause problems.
When this method is called, the control is removed from Page.Validators resulting in the call to Page.IsValid to only be checking the controls that you are about for that button click/command event. Below is an example of removing controls that do not pertain to the button click event.
// remove the correspondence record validators since
// they do not need to be valid for this postback
_tbxDate.RemoveValidation();
_ddlType.RemoveValidation();
_ddlReason.RemoveValidation();
_tbxOtherReason.RemoveValidation();if (Page.IsValid)
{
  ...
}On the next postback, the controls whose RemoveValidation method was called will be automatically added back into the Page.Validators collection.
AddValidation
Use this method to add a control to the Page.Validators collection. This method should really only be called if RemoveValidation was called and then the control needs to be validated within the same postback. After calling this method, you will need to call Page.Validate() to get the validation status of the control.
- AdvancedDate