Slip 5-SPA

 Using angular 13 create a SPA to carry out validation for a username entered in a textbox. If the

textbox is blank, alert “Enter username”. If the number of characters is less than three, alert ‟

Username is too short”. If value entered is appropriate the print “Valid username” and

password should be minimum 8 characters.

app.component.ts

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html'
})
export class AppComponent {

  username: string = '';
  password: string = '';
  message: string = '';

  validateForm() {

    if (this.username.trim() === '') {
      alert("Enter username");
      return;
    }

    if (this.username.length < 3) {
      alert("Username is too short");
      return;
    }

    if (this.password.length < 8) {
      alert("Password must be minimum 8 characters");
      return;
    }

    this.message = "Valid username";
  }

}

app.component.html
<h2>User Validation SPA</h2>

<form (ngSubmit)="validateForm()">

<label>Username:</label>
<br>
<input type="text"
       [(ngModel)]="username"
       name="username"
       required>
<br><br>

<label>Password:</label>
<br>
<input type="password"
       [(ngModel)]="password"
       name="password"
       required>
<br><br>

<button type="submit">
Validate
</button>

</form>

<br>

<p>{{message}}</p>

Comments

Popular posts from this blog

Slip 11: Write node js application that transfer a file as an attachment on web and enables browser to prompt the user to download file using express js.

Slip 7 Create a node js file named main.js for event-driven application. There should be a main loop that listens for events, and then triggers a callback function when one of those events is detected.

Slip 1(a) Write an Angular 13 application addition of two numbers using ng-init, ng-model & ng-bind. And also demonstrate ng-show, ng-disabled, ng-click directives on button component.