<!DOCTYPE html>
<html>
<head>
<base href="." />
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<script type="text/javascript" charset="utf-8">
window.AngularVersionForThisPlunker = 'latest'
</script>
<title>angular playground</title>
<link rel="stylesheet" href="style.css" />
<script src="https://unpkg.com/core-js@2.4.1/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.8.12/dist/zone.js"></script>
<script src="https://unpkg.com/zone.js@0.8.12/dist/long-stack-trace-zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<my-app>
loading...
</my-app>
</body>
</html>
/* Styles go here */
body{
background-color: #9A9BCE;
}
.invalidData1 {
color: red;
font-size: 14px;
}
.invalidData2 {
background-color: #E98787;
color: white;
}
.form {
padding: 5px;
border: 1px solid #f2f2f2;
background-color: #f2f2f2;
border-radius: 5px;
}
### Angular Starter Plunker - Typescript
This Plunker example shows how to validate user input in the UI and display
useful validation messages using Template Driven Forms approach. It is
also an example of Angular 2-way data binding
### https://www.learn-angular2.com ###
### Author : Abhishek Jha ###
var angularVersion;
if(window.AngularVersionForThisPlunker === 'latest'){
angularVersion = ''; //picks up latest
}
else {
angularVersion = '@' + window.AngularVersionForThisPlunker;
}
System.config({
//use typescript for compilation
transpiler: 'typescript',
//typescript compiler options
typescriptOptions: {
emitDecoratorMetadata: true
},
paths: {
'npm:': 'https://unpkg.com/'
},
//map tells the System loader where to look for things
map: {
'app': './src',
'@angular/core': 'npm:@angular/core'+ angularVersion + '/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common' + angularVersion + '/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http' + angularVersion + '/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router' + angularVersion +'/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms' + angularVersion + '/bundles/forms.umd.js',
'@angular/animations': 'npm:@angular/animations' + angularVersion + '/bundles/animations.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations' + angularVersion + '/bundles/animations-browser.umd.js',
'@angular/core/testing': 'npm:@angular/core' + angularVersion + '/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common' + angularVersion + '/bundles/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler' + angularVersion + '/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser' + angularVersion + '/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic' + angularVersion + '/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http' + angularVersion + '/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router' + angularVersion + '/bundles/router-testing.umd.js',
'tslib': 'npm:tslib@1.6.1',
'rxjs': 'npm:rxjs',
'typescript': 'npm:typescript@2.2.1/lib/typescript.js'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
}
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';
platformBrowserDynamic().bootstrapModule(AppModule)
//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
templateUrl: `src/app.html`,
})
export class App {
model1 = {
username: "",
email: "",
comment: ""
};
model2 = {
username: "",
email: "",
comment: ""
};
constructor() {
}
}
@NgModule({
imports: [ BrowserModule, FormsModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
<p><b>Note: Enter the data to see the difference between both the forms.</b></p>
<div class="w3-col l6 m8 s12 form">
<h3>First Form</h3>
<form class="w3-container" name="contactForm1" #contactForm1="ngForm" method="POST" novalidate>
<p>
<label for="username">Name</label>
<input class="w3-input" type="text" name="username1" id="name" required minlength="4" maxlength="24" [(ngModel)]="model1.username" #username1="ngModel"></p>
<div *ngIf="username1.errors && (username1.dirty || username1.touched)" class="invalidData1">
<div [hidden]="!username1.errors.required">
Name is required
</div>
<div [hidden]="!username1.errors.minlength">
Name must be at least 4 characters long.
</div>
<div [hidden]="!username1.errors.maxlength">
Name cannot be more than 24 characters long.
</div>
</div>
<p>
<label>Email</label>
<input class="w3-input" type="email" name="email1" id="email" required minlength="4" maxlength="30" [(ngModel)]="model1.email" #email1="ngModel"></p>
<div *ngIf="email1.errors && (email1.dirty || email1.touched)" class="invalidData1">
<div [hidden]="!email1.errors.required">
Email is required
</div>
<div [hidden]="!email1.errors.minlength">
Email must be at least 4 characters long.
</div>
<div [hidden]="!email1.errors.maxlength">
Email cannot be more than 30 characters long.
</div>
</div>
<p>
<label>Comment</label>
<textarea class="w3-input" rows="4" cols="50" id="comment" required minlength="4" maxlength="500" name="comment1" [(ngModel)]="model1.comment" #comment1="ngModel">
</textarea></p>
<div *ngIf="comment1.errors && (comment1.dirty || comment1.touched)" class="invalidData1">
<div [hidden]="!comment1.errors.required">
Comment is required
</div>
<div [hidden]="!comment1.errors.minlength">
Comment must be at least 4 characters long.
</div>
<div [hidden]="!comment1.errors.maxlength">
Comment cannot be more than 500 characters long.
</div>
</div>
<button type="submit" class="w3-button w3-round w3-right w3-teal" [disabled]="!contactForm1.form.valid">Submit</button>
</form>
</div>
<div class="w3-col l6 m8 s12 form">
<h3>Second Form</h3>
<form class="w3-container" name="contactForm2" #contactForm2="ngForm" method="POST" novalidate>
<p>
<label for="username">Name</label>
<input class="w3-input" type="text" name="username2" [ngClass]="{'invalidData2': username2.errors && (username2.dirty || username2.touched)}" required minlength="4" maxlength="24" [(ngModel)]="model2.username" #username2="ngModel"></p>
<p>
<label>Email</label>
<input class="w3-input" type="email" name="email2" [ngClass]="{'invalidData2': email2.errors && (email2.dirty || email2.touched)}" required minlength="4" maxlength="30" [(ngModel)]="model2.email" #email2="ngModel"></p>
<p>
<label>Comment</label>
<textarea class="w3-input" rows="4" cols="50" [ngClass]="{'invalidData2': comment2.errors && (comment2.dirty || comment2.touched)}" required minlength="4" maxlength="500" name="comment2" [(ngModel)]="model2.comment" #comment2="ngModel">
</textarea></p>
<button type="submit" class="w3-button w3-round w3-right w3-teal" [disabled]="!contactForm2.form.valid">Submit</button>
</form>
</div>