{"id":39753,"date":"2004-04-19T07:00:00","date_gmt":"2004-04-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/04\/19\/wm_killfocus-is-the-wrong-time-to-do-field-validation\/"},"modified":"2004-04-19T07:00:00","modified_gmt":"2004-04-19T07:00:00","slug":"wm_killfocus-is-the-wrong-time-to-do-field-validation","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20040419-00\/?p=39753","title":{"rendered":"WM_KILLFOCUS is the wrong time to do field validation"},"content":{"rendered":"<p>&#8220;I&#8217;ll do my field validation when I get a WM_KILLFOCUS message.&#8221;\n  This is wrong for multiple reasons.\n  First, you may not get your focus loss message until it&#8217;s too late.\n  Consider a dialog box with an edit control and an OK button.  The edit control validates its contents on receipt of the  WM_KILLFOCUS message.  Suppose the user fills in some invalid data.\n  Under favorable circumstances,  the user clicks the OK button.  Clicking the OK button causes focus to move away from the  edit control, so the edit control&#8217;s WM_KILLFOCUS runs and  gets a chance to tell the user that the field is no good.  Since button clicks do not fire until the mouse is released  while still over the button, invalid data will pop up a  message box, which steals focus, and now the mouse-button-release  doesn&#8217;t go to the button control.  Result: Error message and IDOK action does not execute.\n  Now let&#8217;s consider less favorable circumstances.  Instead of clicking on the OK button, the user just  presses Enter or types the keyboard accelerator for  whatever button dismisses the dialog.  The accelerator is converted by IsDialogMessage into a WM_COMMAND  with the button control ID. Focus <strong>does not change<\/strong>.\n  So now the IDOK (or whatever) handler runs and calls EndDialog()  or performs whatever action the button represents.  If the dialog  exits, then focus will leave the edit control as part of dialog box  destruction, and only then will the validation occur, but it&#8217;s  too late now.  The dialog is already exiting.\n  Alternatively, if the action in response to the button is not  dialog termination but rather starting some other procedure,  then it will do it based on the unvalidated data in the dialog box,  which is likely not what you want.  Only when that procedure  moves focus (say, by displaying a progress dialog) will the  edit control receive a WM_KILLFOCUS, at which time it is too  late to do anything. The procedure (using the unvalidated data)  is already under way.\n  There is also a usability problem with validating on focus loss.  Suppose the user starts typing data into the edit control,  and then the user gets distracted.  Maybe they need to open a  piece of email that has the information they need.  Maybe they  got a phone call and need to look up something in their Contacts  database.  Maybe they went to the bathroom and the screen saver  just kicked in.  The user does not want a &#8220;Sorry, that partial  information you entered is invalid&#8221; error dialog, because they  aren&#8217;t yet finished entering the data.\n  I&#8217;ve told you all the places you shouldn&#8217;t do validation but  haven&#8217;t said where you <strong>should<\/strong>.\n  Do the validation when the users indicate that they are done with  data entry and want to go on to the next step.  For a simple  dialog, this would mean performing validation when the OK  or other action verb button is clicked.  For a wizard, it would  be when the Next button is clicked.  For a tabbed dialog, it  would be when the user tabs to a new page.<\/p>\n<p>  (Warnings that do not change focus are permitted, like  the balloon tip that apperas if you accidentally turn on  Caps Lock while typing your password.)  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;I&#8217;ll do my field validation when I get a WM_KILLFOCUS message.&#8221; This is wrong for multiple reasons. First, you may not get your focus loss message until it&#8217;s too late. Consider a dialog box with an edit control and an OK button. The edit control validates its contents on receipt of the WM_KILLFOCUS message. Suppose [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-39753","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>&#8220;I&#8217;ll do my field validation when I get a WM_KILLFOCUS message.&#8221; This is wrong for multiple reasons. First, you may not get your focus loss message until it&#8217;s too late. Consider a dialog box with an edit control and an OK button. The edit control validates its contents on receipt of the WM_KILLFOCUS message. Suppose [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=39753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/39753\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=39753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=39753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=39753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}