{"id":3393,"date":"2011-09-09T10:41:00","date_gmt":"2011-09-09T18:41:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3393"},"modified":"2011-09-12T13:12:15","modified_gmt":"2011-09-12T21:12:15","slug":"primalforms-2011-validating-the-form-part-2","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/09\/09\/primalforms-2011-validating-the-form-part-2\/","title":{"rendered":"PrimalForms 2011: Validating the Form &#8211; Part 2"},"content":{"rendered":"<p><font size=\"3\">In <a href=\"http:\/\/www.sapien.com\/blog\/2011\/09\/02\/primalforms-2011-validating-the-form-part-1\/\">Part 1<\/a> we discussed <\/font>the import events and properties that are needed for validation. Now we will discuss how to validate a control.&nbsp; <\/p>\n<p><strong><font size=\"3\">How do I validate a control?<\/font><\/strong><\/p>\n<p>As it turns out, validating controls depends on what type of control and the format of the data you wish to validate. Typically you will be validating text that is provided by a TextBox, but it could also come from a ComboBox where the user can type their selection. As for validating the text, it can range from simply ensuring there is value (i.e., not an empty string) or more complex task such as validating the format of an email address. The bad news is that you have to script your validation and it can change depending on the data type and format. <\/p>\n<p>Tip: Use functions and code snippets. Never write the same validation function twice! Once you have written a validation function for a specific format, convert it into a snippet, so the next time you need to validate similar data, you will save time by simply inserting your snippet.<\/p>\n<p><em>Validation Example 1: Checking if a TextBox Field is Empty<\/em><\/p>\n<p>For the first example, we will create a simple function that will ensure the field is empty as well as making sure it doesn\u2019t only contain blank spaces.<\/p>\n<pre><span style=\"color: #0000ff\">function<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> ([string] <\/span><span style=\"color: #8b0000\">$field<\/span><span style=\"color: #000000\">)\n{\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$field<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-or<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$field<\/span><span style=\"color: #000000\">.Trim().Length <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #000000\">0<\/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    }\n    \n    <\/span><span style=\"color: #0000ff\">return<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n}<\/span><\/pre>\n<p>Next add a Validating event to the TextBox:<\/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\">#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]<\/span><span style=\"color: #000000\">\n    \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: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">.Text\n}<\/span><\/pre>\n<p>In this example, we cancel the validation, when the field is empty or has only whitespaces. Since we are canceling, the focus will remain on the TextBox until the user enters the correct format.<\/p>\n<p><strong>How do I let the user know there is a validation failure?<\/strong><\/p>\n<p>You can use various techniques to notify a user. You can have a label to display the error message or change the foreground color of the TextBox or use a ToolTip control to display a popup message. A good solution is to use the ErrorProvider control. It displays an icon next to the control indicating an error as well as displaying an error message when the user hovers the mouse over the icon. For more details on the ErrorProvider control, refer to the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/08\/18\/primalforms-2011-spotlight-on-the-errorprovider-control\/\">PrimalForms 2011: Spotlight on the ErrorProvider<\/a> blog article. <\/p>\n<p>Let\u2019s update Example 1 to display an Error Message using an ErrorProvider Control:<\/p>\n<pre><span style=\"color: #8b0000\">$textboxName_Validating<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.ComponentModel.CancelEventHandler]{\n\n    <\/span><span style=\"color: #008000\">#Check if the Name field is empty<\/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: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">.Text    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel)\n    {\n        <\/span><span style=\"color: #008000\">#Display an error message<\/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\">, <\/span><span style=\"color: #ff0000\">\"Please enter your name.\"<\/span><span style=\"color: #000000\">);\n    }\n}<\/span><\/pre>\n<p>Next add a Validated event to clear in the ErrorProvider\u2019s message. Since our event will only clear the error message, it can be used as a generic event shared by all validating controls. This is done by passing <span style=\"color: #8b0000\">$this<\/span> variable to the ErrorProvider\u2019s SetError method. The <span style=\"color: #8b0000\">$this<\/span> variable represents the control that is calling the event.<\/p>\n<pre><span style=\"color: #8b0000\">$control_Validated<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">{\n    <\/span><span style=\"color: #008000\">#Pass the calling control and clear error message<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$errorprovider1<\/span><span style=\"color: #000000\">.SetError(<\/span><span style=\"color: #8b0000\">$this<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #ff0000\">\"\"<\/span><span style=\"color: #000000\">);    \n}<\/span><\/pre>\n<p><strong>Reminder: The Validated event will not be called if <span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span>was set to <span style=\"color: #8b0000\">$True<\/span> in the Validating event.<\/strong><\/p>\n<p>If you don\u2019t want to use the <span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span>property in the Validating event (because users may not like it when they cannot select another field when the validation fails), then it is recommended to remove the Validated event and update the Validating event as follows:<\/p>\n<pre><span style=\"color: #8b0000\">$textboxName_Validating<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.ComponentModel.CancelEventHandler]{\n\n    <\/span><span style=\"color: #008000\">#Check if the Name field is empty<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$result<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #008080\">Validate-IsEmptyTrim<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textboxName<\/span><span style=\"color: #000000\">.Text    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$result<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #008000\">#Display an error message<\/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\">, <\/span><span style=\"color: #ff0000\">\"Please enter your name.\"<\/span><span style=\"color: #000000\">);\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #008000\">#Clear the error message<\/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\">, <\/span><span style=\"color: #ff0000\">\"\"<\/span><span style=\"color: #000000\">);\n    }\n}<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p>Next: Part 3 \u2013 Other Validating Techniques <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Part 1 we discussed the import events and properties that are needed for validation. Now we will discuss how to validate a control.&nbsp; How do I validate a control? As it turns out, validating controls depends on what type of control and the format of the data you wish to validate. Typically you will [&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-3393","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\/3393","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=3393"}],"version-history":[{"count":8,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3393\/revisions"}],"predecessor-version":[{"id":3446,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3393\/revisions\/3446"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}