<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Angular Validations &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/angular-validations/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Tue, 10 Jul 2018 07:39:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>Angular Validations &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Implement Validations in Angular 5 App</title>
		<link>https://www.sibeeshpassion.com/implement-validations-in-angular-5-app/</link>
					<comments>https://www.sibeeshpassion.com/implement-validations-in-angular-5-app/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Sat, 02 Dec 2017 19:50:08 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Angular 5]]></category>
		<category><![CDATA[Angular 5 Validations]]></category>
		<category><![CDATA[Angular Validations]]></category>
		<category><![CDATA[Simple Angular Validation]]></category>
		<category><![CDATA[Two way binding in Angular]]></category>
		<guid isPermaLink="false">http://sibeecst_passion.com/?p=12559</guid>

					<description><![CDATA[[toc] Introduction This post is a continuation of the course Developing an Angular 5 App series if you haven&#8217;t gone through the previous posts yet, I strongly recommend you to do that. You can find the links to the previous posts below. In this post, we are going to implement Two Way binding and validations in  Angular 5 registration form. So at the end of this article, you will be proficient in how to do validations in Angular. I hope you will like this article. Developing an Angular 5 App series These are the previous posts in this series. Please [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>[toc]</p>
<h3>Introduction</h3>
<p>This post is a continuation of the course <em>Developing an Angular 5 App</em> series if you haven&#8217;t gone through the previous posts yet, I strongly recommend you to do that. You can find the links to the previous posts below. In this post, we are going to implement Two Way binding and validations in  <a href="http://sibeeshpassion.com/category/client-side-technologies/angular/">Angular</a> 5 registration form. So at the end of this article, you will be proficient in how to do validations in Angular. I hope you will like this article.</p>
<h3><em>Developing an Angular 5 App</em> series</h3>
<p>These are the previous posts in this series. Please go ahead and have a look.</p>
<ol>
<li><a href="http://sibeeshpassion.com/what-is-new-and-how-to-set-up-our-first-angular-5-application/">What Is New and How to Set Up our First Angular 5 Application</a></li>
<li><a href="http://sibeeshpassion.com/angular-5-basic-demo-project-overview/">Angular 5 Basic Demo Project Overview</a></li>
<li><a href="http://sibeeshpassion.com/generating-your-first-components-and-modules-in-angular-5-app/">Generating Your First Components And Modules in Angular 5 App</a></li>
<li><a href="http://sibeeshpassion.com/implement-validations-in-angular-5-app/">Implement Validations in Angular 5 App</a></li>
</ol>
<h3><span id="source-code">Source Code</span></h3>
<p>You can always clone or download the source code <a href="https://github.com/SibeeshVenu/ng5">here</a></p>
<h3>Background</h3>
<p>Validations are having a vital role in all the applications, without validation, anyone can push invalid data to your application. So here we are also going to implement some validations, it is our application and we want to make it perfect right?</p>
<h3>Validation in Angular</h3>
<p>To get started with the Angular forms, we need to import some modules to our app.module.ts</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">import { FormsModule, ReactiveFormsModule } from '@angular/forms'</pre>
<pre class="EnlighterJSRAW" data-enlighter-language="js">@NgModule({
  declarations: [
    AppComponent,
    RegistrationComponent,
    HomeComponent,
    NavComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    AppRoutingModule,
    MatButtonModule, MatCardModule, MatInputModule, MatSnackBarModule, MatToolbarModule,
    FormsModule, ReactiveFormsModule,
    RouterModule.forRoot(myRoots)
  ],
  providers: [],
  bootstrap: [AppComponent]
})</pre>
<p>In our previous post, we have developed an Angular form in the component named Registration. Now let us open <em>registration.component.ts </em>file and import FormBuilder, Validators in it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">import { FormBuilder, Validators } from '@angular/forms';</pre>
<p>Let&#8217;s inject the FormBuilder in our constructor.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">constructor(private fb: FormBuilder) {}</pre>
<p>Next, is building a form group, so that we can include our model in it.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">form;
  constructor(private fb: FormBuilder) {
    this.form = fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', Validators.required],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    });
  }</pre>
<p>In the group model, the first argument is the initial value that you may need to show. You can always do as preceding.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">firstName: ['Sibeesh', Validators.required]</pre>
<p>We will have to do some more changes in our registration.component.html.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">&lt;mat-card&gt;
 &lt;form [formGroup]="form"&gt;
 &lt;mat-input-container&gt;
 &lt;input matInput placeholder="First Name" formControlName="firstName" /&gt;
 &lt;/mat-input-container&gt;
 &lt;mat-input-container&gt;
 &lt;input matInput placeholder="Last Name" formControlName="lastName" /&gt;
 &lt;/mat-input-container&gt;
 &lt;mat-input-container&gt;
 &lt;input matInput type="email" placeholder="Email" formControlName="email" /&gt;
 &lt;/mat-input-container&gt;
 &lt;mat-input-container&gt;
 &lt;input matInput type="password" placeholder="Password" formControlName="password" /&gt;
 &lt;/mat-input-container&gt;
 &lt;mat-input-container&gt;
 &lt;input matInput type="password" placeholder="Confirm Password" formControlName="confirmPassword" /&gt;
 &lt;/mat-input-container&gt;
 &lt;button mat-raised-button color="primary"&gt;Register&lt;/button&gt;
 &lt;/form&gt;
&lt;/mat-card&gt;
</pre>
<p>Here <em>formControlName </em>is the tag which connects our model value and the control, so if you are not providing this value in your HTML, the validation for that particular control will not work. Now let us run our application and see the output.</p>
<div id="attachment_12561" style="width: 644px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/11/Angular_Form_With_Validation-e1510909875736.png"><img fetchpriority="high" decoding="async" aria-describedby="caption-attachment-12561" class="size-full wp-image-12561" src="http://sibeeshpassion.com/wp-content/uploads/2017/11/Angular_Form_With_Validation-e1510909875736.png" alt="Angular_Form_With_Validation" width="634" height="309" /></a><p id="caption-attachment-12561" class="wp-caption-text">Angular_Form_With_Validation</p></div>
<p>Please be noted that we have given value only for the field First Name, so the remaining fields are showing in red color when we click our Register button. So our validation is working as expected.</p>
<p>You can always apply some custom validations too, like Email field validators. Let&#8217;s do that now.  Please change your constructor as preceding.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">constructor(private fb: FormBuilder, private auth: AuthService) {
    this.form = fb.group({
      firstName: ['', Validators.required],
      lastName: ['', Validators.required],
      email: ['', [Validators.required, isEmailValid('email')]],
      password: ['', Validators.required],
      confirmPassword: ['', Validators.required]
    });
  }</pre>
<p>Now as you guessed, we need to implement the function <strong>isEmailValid</strong>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="js">function isEmailValid(control) {
  return control =&gt; {
    var regex = /^(([^&lt;&gt;()\[\]\\.,;:\s@"]+(\.[^&lt;&gt;()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return regex.test(control.value) ? null : { invalidEmail: true };
  }
}</pre>
<p>Now that we have done our custom validation, let&#8217;s check it out in our application.</p>
<div id="attachment_12584" style="width: 624px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/12/Custom-Email-Field-Validator-in-Angular-5-App.png"><img decoding="async" aria-describedby="caption-attachment-12584" class="size-full wp-image-12584" src="http://sibeeshpassion.com/wp-content/uploads/2017/12/Custom-Email-Field-Validator-in-Angular-5-App.png" alt="Custom Email Field Validator in Angular 5 App" width="614" height="537" srcset="/wp-content/uploads/2017/12/Custom-Email-Field-Validator-in-Angular-5-App.png 408w, /wp-content/uploads/2017/12/Custom-Email-Field-Validator-in-Angular-5-App-300x262.png 300w, /wp-content/uploads/2017/12/Custom-Email-Field-Validator-in-Angular-5-App-400x350.png 400w" sizes="(max-width: 614px) 100vw, 614px" /></a><p id="caption-attachment-12584" class="wp-caption-text">Custom Email Field Validator in Angular 5 App</p></div>
<p>Let&#8217;s create a click event to our register button and get the form values that we have typed, so that we can pass these values to our server and save the same in our next article.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="html">&lt;button mat-raised-button (click)="register()" color="primary"&gt;Register&lt;/button&gt;</pre>
<pre class="EnlighterJSRAW" data-enlighter-language="js"> register() {
    console.log(this.form.value);
}</pre>
<p>Please make sure that you are getting the values in your browser console.</p>
<div id="attachment_12585" style="width: 362px" class="wp-caption alignnone"><a href="http://sibeeshpassion.com/wp-content/uploads/2017/12/Get-the-form-values-e1512243815962.png"><img decoding="async" aria-describedby="caption-attachment-12585" class="size-full wp-image-12585" src="http://sibeeshpassion.com/wp-content/uploads/2017/12/Get-the-form-values-e1512243815962.png" alt="Get the form values" width="352" height="357" /></a><p id="caption-attachment-12585" class="wp-caption-text">Get the form values</p></div>
<p><em> </em></p>
<p>That&#8217;s all for today. In our next article, we can do some database actions, be ready.</p>
<h3><span id="conclusion">Conclusion</span></h3>
<p>Thanks a lot for reading. Did I miss anything that you may think which is needed? Could you find this post as useful? I hope you liked this article. Please share me your valuable suggestions and feedback.</p>
<h3><span id="your-turn-what-do-you-think">Your turn. What do you think?</span></h3>
<p>A blog isn’t a blog without comments, but do try to stay on topic. If you have a question unrelated to this post, you’re better off posting it on C# Corner, Code Project, Stack Overflow, Asp.Net Forum instead of commenting here. Tweet or email me a link to your question there and I’ll definitely try to help if I can.</p>
<p>Kindest Regards<br />
Sibeesh Venu</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/implement-validations-in-angular-5-app/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
