PrimalForms 2011: Validating the Form – Part 3

In Part 2 we discussed how to validate a control. Now we will cover some techniques that can be utilize for validation.

Validating Techniques:

Regular Expressions:

For more complex validation, such as validating an email address, you may need to rely on Regular Expressions. Regular expressions are useful because it allows you to match complex data by providing formatting information. In addition, PowerShell has built in support for regular expressions in the form of the –match operator.

Example use of a regular expression in PowerShell:

“PowerShell” -match “shell”

In this example we are matching any string that contains the word “shell”.

We will not go into details on how to use Regular Expressions, since it’s an extensive subject unto itself. We recommend referring to PowerShell’s about_Regular_Expressions help topic or type “PowerShell Regular Expressions” in a search engine for more information.

Leverage PowerShell’s Parameter Validation:

By using Parameter validation you can as validate TextBox values as well; although this isn’t necessary the best method. For more information on Parameter Validation refer to the about_functions_advanced_parameters help topic.

Validating Example 2 – Using Parameter Validation

First, define a function that will use the function’s Parameter Validation attributes:

function ParameterValidate 
    return $true

Next modify the TextBox’s Validating event:

    #Init to False in case Validate Fails
    $_.Cancel = $true
    #Use Parameter Validation to Check TextBox value
        $_.Cancel = -not(ParameterValidate $textboxName.Text)
    catch #Catch any Parameter Errors
        #$_ is now an Error in this scope
        $errorprovider1.SetError($textboxName, [string]$_.Exception.Message);

The resulting Error Message when the TextBox is empty:

Cannot validate argument on parameter ‘Text’. The number of characters (0) in the argument is too small. Specify an argument whose length is greater than or equal to “1” and then try the command again.

As you can see this may not be the best error message to present to a user; therefore, you may have to customize your error messages.

Utilize the Appropriate Controls:

One can reduce the need to validate by using the appropriate controls for your data types:


Use this control when you need a date or time. This control will inherently validate the date field for you; therefore, you need not parse the date or use regular expressions,


Use this control when you need a phone number or other set format. The MaskTextBox insures the field is entered correctly by enforcing formatting. Depending on the format, you may still need custom validation for control or use the MaskTextBox’s built in validation for DateTime formats and number values. See PrimalForms 2011: Spotlight on the MaskedTextBox Control blog article for more details.


Use this control when you have a predetermined set of values that the user must select from.


Use this control when you have yes or no type question.


Use this control when you have integer values. This control allows you to specify a range of valid values.


Use this control when you have text information such as a name. If there is a size constraint on the data, then use the TextBox’s the MaxLength property to limit the number of characters the user can enter. See PrimalForms 2011: Spotlight on the TextBox Control blog article for more details.

Next: Part 4 – How to trigger validation when the form is closing