{"id":3394,"date":"2011-09-21T07:33:00","date_gmt":"2011-09-21T15:33:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3394"},"modified":"2011-09-27T08:56:31","modified_gmt":"2011-09-27T16:56:31","slug":"primalforms-2011-validating-the-form-part-3","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/09\/21\/primalforms-2011-validating-the-form-part-3\/","title":{"rendered":"PrimalForms 2011: Validating the Form &#8211; Part 3"},"content":{"rendered":"<p>In <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/09\/primalforms-2011-validating-the-form-part-2\/\">Part 2<\/a> we discussed how to validate a control. Now we will cover some techniques that can be utilize for validation.  <\/p>\n<p><strong>Validating Techniques:<\/strong><\/p>\n<p><em><strong>Regular Expressions:<\/strong><\/em><\/p>\n<p>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 \u2013match operator. <\/p>\n<p>Example use of a regular expression in PowerShell:<\/p>\n<p><span style=\"color: #ff0000\">&#8220;PowerShell&#8221;<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-match<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">&#8220;shell&#8221;<\/span><\/p>\n<p>In this example we are matching any string that contains the word \u201cshell\u201d. <\/p>\n<p>We will not go into details on how to use Regular Expressions, since it\u2019s an extensive subject unto itself. We recommend referring to PowerShell\u2019s <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd315294.aspx\">about_Regular_Expressions<\/a> help topic or type \u201cPowerShell Regular Expressions\u201d in a search engine for more information.<\/p>\n<p><em><strong>Leverage PowerShell\u2019s Parameter Validation:<\/strong><\/em><\/p>\n<p>By using Parameter validation you can as validate TextBox values as well; although this isn\u2019t necessary the best method. For more information on Parameter Validation refer to the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/dd347600.aspx\">about_functions_advanced_parameters<\/a> help topic.<\/p>\n<p><em>Validating Example 2 \u2013 Using Parameter Validation<\/em><\/p>\n<p>First, define a function that will use the function\u2019s Parameter Validation attributes:<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">ParameterValidate<\/span><span style=\"color: #000000\"> \n{\n    <\/span><span style=\"color: #0000ff\">Param<\/span><span style=\"color: #000000\">(\n    [Parameter(Mandatory<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)]\n    [ValidateNotNull()]    \n    [ValidateLength(<\/span><span style=\"color: #000000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #000000\">10<\/span><span style=\"color: #000000\">)]\n    [String]<\/span><span style=\"color: #8b0000\">$Text<\/span><span style=\"color: #000000\">\n    )\n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p>Next modify the TextBox\u2019s Validating event:<\/p>\n<pre><span style=\"color: #8b0000\">$textboxName_Validating<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.ComponentModel.CancelEventHandler]{\n    <\/span><span style=\"color: #008000\">#Init to False in case Validate Fails<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #008000\">#Use Parameter Validation to Check TextBox value<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff\">try<\/span><span style=\"color: #000000\">{\n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-not<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #008080\">ParameterValidate<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">.Text)\n    }\n    <\/span><span style=\"color: #0000ff\">catch<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008000\">#Catch any Parameter Errors<\/span><span style=\"color: #000000\">\n    {    \n        <\/span><span style=\"color: #008000\">#$_ is now an Error in this scope<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$errorprovider1<\/span><span style=\"color: #000000\">.SetError(<\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">, [string]<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Exception.Message);\n    }\n}<\/span><\/pre>\n<p>The resulting Error Message when the TextBox is empty:<\/p>\n<p><span style=\"color: #ff0000\">Cannot validate argument on parameter &#8216;Text&#8217;. The number of characters (0) in the argument is too small. Specify an argument whose length is greater than or equal to &#8220;1&#8221; and then try the command again.<\/span> <\/p>\n<p>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.<\/p>\n<p><em><strong>Utilize the Appropriate Controls:<\/strong><\/em><\/p>\n<p>One can reduce the need to validate by using the appropriate controls for your data types:<\/p>\n<blockquote>\n<p><strong>DateTimePicker<\/strong> <\/p>\n<p>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,<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>MaskTextBox<\/strong><\/p>\n<p>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\u2019s built in validation for DateTime formats and number values. See <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/27\/primalforms-2011-spotlight-on-the-maskedtextbox-control\/\">PrimalForms 2011: Spotlight on the MaskedTextBox Control<\/a> blog article for more details.<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>ComboBox<\/strong><\/p>\n<p>Use this control when you have a predetermined set of values that the user must select from.<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>CheckBox<\/strong><\/p>\n<p>Use this control when you have yes or no type question.<\/p>\n<\/blockquote>\n<blockquote>\n<p><strong>NumericUpDown<\/strong><\/p>\n<p>Use this control when you have integer values. This control allows you to specify a range of valid values.<\/p>\n<p><strong>TextBox<\/strong><\/p>\n<p>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\u2019s the MaxLength property to limit the number of characters the user can enter. See <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/13\/primalforms-2011-spotlight-on-the-textbox-control\/\">PrimalForms 2011: Spotlight on the TextBox Control<\/a> blog article for more details. <\/p>\n<\/blockquote>\n<p>Next: <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/23\/primalforms-2011-validating-the-form-part-4\/\">Part 4<\/a> \u2013 How to trigger validation when the form is closing<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,25],"tags":[313,28,633,680,327],"class_list":["post-3394","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-windows-powershell","tag-forms","tag-powershell","tag-primalforms-2011","tag-validation","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3394","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3394"}],"version-history":[{"count":10,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3394\/revisions"}],"predecessor-version":[{"id":3481,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3394\/revisions\/3481"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}