{"id":541,"date":"2023-02-22T05:00:00","date_gmt":"2023-02-22T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=541"},"modified":"2023-02-21T15:53:05","modified_gmt":"2023-02-21T23:53:05","slug":"custom-form-validation-functions","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/custom-form-validation-functions\/","title":{"rendered":"Custom form validation functions"},"content":{"rendered":"\n<p>Let&#8217;s continue our dive into Angular form validation capabilities. So far, we have seen <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/implementing-custom-feedback-to-form-validation-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">how to display custom feedback<\/a> to the user based on <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/basic-form-validation-with-angular\/\" target=\"_blank\" rel=\"noopener\" title=\"\">basic HTML validation features<\/a>. Today, let&#8217;s see how to customize the validation itself.<\/p>\n\n\n\n<p>The good news is that all we need is a function. That function takes a <a style=\"\" href=\"https:\/\/angular.io\/api\/forms\/AbstractControl\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>FormControl<\/code><\/a> as a parameter and returns <code>null<\/code> if the value entered by the user is valid. If the value is invalid, we return a ValidationErrors object, any key\/value object we want.<\/p>\n\n\n\n<p>Here&#8217;s an example:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"846\" height=\"163\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-47.png\" alt=\"\" class=\"wp-image-543\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-47.png 846w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-47-300x58.png 300w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-47-768x148.png 768w\" sizes=\"auto, (max-width: 846px) 100vw, 846px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Pretty straightforward, right? If the zip code is wrong, we return an object with a custom error message. Otherwise, we return <code>null<\/code>.<\/p>\n\n\n\n<p>Of course, we can validate more precisely by adding several different checks and specific error messages for each case:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"665\" height=\"154\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-48.png\" alt=\"\" class=\"wp-image-544\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-48.png 665w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-48-300x69.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Then to have a specific input use that validation function, we can pass it as a parameter to the <code>FormControl<\/code> constructor like so:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"657\" height=\"32\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-49.png\" alt=\"\" class=\"wp-image-545\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-49.png 657w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-49-300x15.png 300w\" sizes=\"auto, (max-width: 657px) 100vw, 657px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And then such <code>FormControl<\/code> gets bound to the proper input in our HTML template (this is the approach for reactive forms &#8211; we will cover a method that works for template-driven forms tomorrow). We can then add some error handling by using the errors property of our <code>FormControl<\/code>, which is going to have the <code>ValidationErrors<\/code> object returned from our validation function:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"619\" height=\"174\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-50.png\" alt=\"\" class=\"wp-image-546\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-50.png 619w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-50-300x84.png 300w\" sizes=\"auto, (max-width: 619px) 100vw, 619px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now our form provides custom feedback using our custom validation function:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"405\" height=\"121\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-51.png\" alt=\"\" class=\"wp-image-547\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-51.png 405w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-51-300x90.png 300w\" sizes=\"auto, (max-width: 405px) 100vw, 405px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>You can access the code for the <a href=\"https:\/\/stackblitz.com\/edit\/ng-custom-validation-function?file=src%2Fglobal_styles.css,src%2Fmain.ts,src%2Fmain.html\" target=\"_blank\" rel=\"noopener\" title=\"\">above example on Stackblitz.<\/a> <\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s continue our dive into Angular form validation capabilities. So far, we have seen how to display custom feedback to the user based on basic HTML validation features. Today, let&#8217;s see how to customize the validation itself. The good news is that all we need is a function. That function takes a FormControl as a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,22],"tags":[],"class_list":["post-541","post","type-post","status-publish","format-standard","hentry","category-angular","category-forms"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/541","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=541"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/541\/revisions"}],"predecessor-version":[{"id":549,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/541\/revisions\/549"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}