{"id":518,"date":"2023-02-20T05:00:00","date_gmt":"2023-02-20T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=518"},"modified":"2023-02-16T15:34:51","modified_gmt":"2023-02-16T23:34:51","slug":"basic-form-validation-with-angular","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/basic-form-validation-with-angular\/","title":{"rendered":"Basic form validation with Angular"},"content":{"rendered":"\n<p>Validating user input in HTML forms can be a tedious task. In this new series, we&#8217;ll look at how Angular can help us implement painless form validation.<\/p>\n\n\n\n<p>First, it&#8217;s essential to know that Angular relies primarily on native browser validation features that use modern HTML properties. For instance, if a form field has to be filled out, you can mark it as required using the <code>required<\/code> HTML attribute: <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"421\" height=\"25\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-34.png\" alt=\"\" class=\"wp-image-519\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-34.png 421w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-34-300x18.png 300w\" sizes=\"auto, (max-width: 421px) 100vw, 421px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If the user input has to match a specific format, you can specify such format using the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Attributes\/pattern\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>pattern<\/code> attribute<\/a>, which uses a regular expression syntax &#8211; here, a 5-digit number<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"514\" height=\"22\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-35.png\" alt=\"\" class=\"wp-image-520\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-35.png 514w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-35-300x13.png 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Other available HTML validation attributes are <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Attributes\/min\" target=\"_blank\" rel=\"noopener\" title=\"\">min<\/a><\/code>, <code>max<\/code>, <code><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Attributes\/minlength\" target=\"_blank\" rel=\"noopener\" title=\"\">minlength<\/a><\/code>, <code>maxlength<\/code>. You can also set the input type to something more specific than <code>text<\/code>, such as <code>email<\/code> or <code>tel<\/code> for additional validation and capabilities. Here is the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Element\/input#input_types\" target=\"_blank\" rel=\"noopener\" title=\"\">list of all possible input types<\/a>.<\/p>\n\n\n\n<p>Once you have specified your validation rules using such attributes, Angular is going to toggle some CSS classes on the corresponding HTML elements automatically: <code>ng-valid<\/code> when the entered value is valid, and <code>ng-invalid<\/code> when the entered value is invalid.<\/p>\n\n\n\n<p>This means that all we have to do to provide visual feedback to the user is implementing such classes in our CSS stylesheets:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"283\" height=\"185\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-36.png\" alt=\"\" class=\"wp-image-521\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>The above CSS classes result in the following styling of form inputs:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"253\" height=\"84\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-37.png\" alt=\"\" class=\"wp-image-522\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>If we want to fine-tune the rendering of our form based on whether the user has already typed something or not, there are additional classes added by Angular for such purposes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ng-pristine<\/code>: <code>true<\/code> when the user has not changed the element value<\/li>\n\n\n\n<li><code>ng-dirty<\/code>: opposite of pristine \u2014 <code>true<\/code> when the user has altered the element value<\/li>\n\n\n\n<li><code>ng-touched<\/code>: <code>true<\/code> when the user has put focus on the element (say clicked on it) and then removed the focus from the element (clicked away from it)<\/li>\n\n\n\n<li><code>ng-untouched<\/code>: Opposite of touched \u2014 means the user hasn\u2019t put focus on the element or hasn\u2019t removed that focus yet.<\/li>\n<\/ul>\n\n\n\n<p>Using combinations of these classes is powerful. For instance, here is how we could add the error styles only once the user has &#8220;touched&#8221; an input:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"284\" height=\"91\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-38.png\" alt=\"\" class=\"wp-image-523\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Which results in the following default rendering:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"239\" height=\"44\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-39.png\" alt=\"\" class=\"wp-image-524\"\/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>And then once the user has typed something and removed the focus from the element:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"238\" height=\"45\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-40.png\" alt=\"\" class=\"wp-image-525\"\/><\/figure>\n<\/div>\n\n\n<p>You can try a few live examples using <a href=\"https:\/\/stackblitz.com\/github\/alcfeoh\/ng-training-demos?embed=1&amp;file=src\/app\/forms\/template-driven-form.component.ts,src\/app\/forms\/template-driven-form.component.html&amp;initialpath=template-driven-form\" target=\"_blank\" rel=\"noopener\" title=\"\">the code from this Stackblitz repo<\/a>. Tomorrow, we&#8217;ll see how to do more than just CSS styling using those validation properties.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Validating user input in HTML forms can be a tedious task. In this new series, we&#8217;ll look at how Angular can help us implement painless form validation. First, it&#8217;s essential to know that Angular relies primarily on native browser validation features that use modern HTML properties. For instance, if a form field has to be [&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-518","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\/518","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=518"}],"version-history":[{"count":2,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":527,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/518\/revisions\/527"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}