import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: '<app-signup-form> </app-signup-form>'
})
export class AppComponent { }
/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { ReactiveFormsModule } from '@angular/forms';
import { SignupFormComponent } from './signup-form/signup-form.component';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
ReactiveFormsModule
],
declarations: [
AppComponent,
SignupFormComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
/*
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
<!DOCTYPE html>
<html>
<head>
<title>Hero Form</title>
<script>document.write('<base href="' + document.location + '" />');</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://unpkg.com/bootstrap@3.3.7/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="forms.css">
<!-- Polyfills -->
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('main.js').catch(function(err){ console.error(err); });
</script>
<link rel="stylesheet" type="text/css" href="https://bootswatch.com/paper/bootstrap.min.css">
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
<!--
Copyright 2017 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->
/**
* WEB ANGULAR VERSION
* (based on systemjs.config.js in angular.io)
* System configuration for Angular samples
* Adjust as necessary for your application needs.
*/
(function (global) {
System.config({
// DEMO ONLY! REAL CODE SHOULD NOT TRANSPILE IN THE BROWSER
transpiler: 'ts',
typescriptOptions: {
// Copy of compiler options in standard tsconfig.json
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/'
},
// map tells the System loader where to look for things
map: {
// our app is within the app folder
'app': 'app',
// angular bundles
'@angular/animations': 'npm:@angular/animations/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations/bundles/animations-browser.umd.js',
'@angular/core': 'npm:@angular/core/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common/bundles/common.umd.js',
'@angular/common/http': 'npm:@angular/common/bundles/common-http.umd.js',
'@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser/bundles/platform-browser-animations.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router/bundles/router.umd.js',
'@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
'@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs@5.0.1',
'tslib': 'npm:tslib/tslib.js',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api@0.4/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.3.2/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts',
meta: {
'./*.ts': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/
var templateUrlRegex = /templateUrl\s*:(\s*['"`](.*?)['"`]\s*)/gm;
var stylesRegex = /styleUrls *:(\s*\[[^\]]*?\])/g;
var stringRegex = /(['`"])((?:[^\\]\\\1|.)*?)\1/g;
module.exports.translate = function(load){
if (load.source.indexOf('moduleId') != -1) return load;
var url = document.createElement('a');
url.href = load.address;
var basePathParts = url.pathname.split('/');
basePathParts.pop();
var basePath = basePathParts.join('/');
var baseHref = document.createElement('a');
baseHref.href = this.baseURL;
baseHref = baseHref.pathname;
if (!baseHref.startsWith('/base/')) { // it is not karma
basePath = basePath.replace(baseHref, '');
}
load.source = load.source
.replace(templateUrlRegex, function(match, quote, url){
var resolvedUrl = url;
if (url.startsWith('.')) {
resolvedUrl = basePath + url.substr(1);
}
return 'templateUrl: "' + resolvedUrl + '"';
})
.replace(stylesRegex, function(match, relativeUrls) {
var urls = [];
while ((match = stringRegex.exec(relativeUrls)) !== null) {
if (match[2].startsWith('.')) {
urls.push('"' + basePath + match[2].substr(1) + '"');
} else {
urls.push('"' + match[2] + '"');
}
}
return "styleUrls: [" + urls.join(', ') + "]";
});
return load;
};
export class User {
id: number;
email: string;
//Both the passwords are in a single object
password: {
pwd: string;
confirmPwd: string;
};
gender: string;
terms: boolean;
constructor(values: Object = {}) {
//Constructor initialization
Object.assign(this, values);
}
}
import { Component, OnInit } from '@angular/core';
// Import the User model
import { User } from './../User';
import { passwordMatch } from './../password-match';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'app-signup-form',
templateUrl: './signup-form.component.html',
styleUrls: ['./signup-form.component.css']
})
export class SignupFormComponent implements OnInit {
//Inject the formbuilder into the constructor
constructor(private fb:FormBuilder) {}
//Property for the user
private user:User;
//Gender list for the select control element
genderList: string[];
signupForm: FormGroup;
ngOnInit() {
this.genderList = ['Male', 'Female', 'Others'];
// Use the formbuilder to build the Form model
this.signupForm = this.fb.group({
email: ['',[Validators.required,
Validators.pattern('[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,3}$')]],
password: this.fb.group({
pwd: ['', [Validators.required,
Validators.minLength(8)]],
confirmPwd: ['', [Validators.required,
Validators.minLength(8)
]]}, { validator: passwordMatch }),
gender: ['', Validators.required],
terms: ['', Validators.requiredTrue]
})
}
get email() { return this.signupForm.get('email'); }
get password() { return this.signupForm.get('password'); }
get gender() { return this.signupForm.get('gender'); }
get terms() { return this.signupForm.get('terms'); }
public onFormSubmit() {
if(this.signupForm.valid) {
this.user = this.signupForm.value;
console.log(this.user);
/* Any API call logic via services goes here */
}
}
}
<div class="row">
<div class= "col-sm-7 custom-row jumbotron">
<form class="form-horizontal" [formGroup]="signupForm"
(ngSubmit)="onFormSubmit()">
<fieldset>
<legend>SignUp</legend>
<!--- Email Block -->
<div class="form-group">
<label for="inputEmail" class="col-lg-2
control-label">Email</label>
<div class="col-lg-7">
<input type="text" class="form-control"
id="inputEmail" formControlName = "email"
placeholder="Email">
</div>
<!-- Email error block -->
<div *ngIf="email.invalid && email.touched"
class="col-sm-3 text-danger">
<div *ngIf = "email.errors?.required">
Email field can't be blank
</div>
<div *ngIf = "email.errors?.pattern">
The email id doesn't seem right
</div>
</div>
</div>
<!--- Password Block -->
<div formGroupName = "password">
<div class="form-group">
<label for="inputPassword" class="col-lg-2 control-label">Password</label>
<div class="col-lg-7">
<input type="password" formControlName = "pwd"
id="inputPassword" class="form-control"
placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="confirmPassword" class="col-lg-2 control-label">Confirm Password</label>
<div class="col-lg-7">
<input type="password" formControlName = "confirmPwd"
id="confirmPassword" class="form-control"
placeholder="Password">
</div>
<!-- Password error block -->
</div>
<div *ngIf="(password.invalid && password.touched)" class="col-sm-3 text-danger">
<ng-container *ngIf="password.errors?.mismatch;
then first else second"> </ng-container>
<ng-template #first>
Password do not match </ng-template>
<ng-template #second>
Password needs to be more than 8 characters
</ng-template>
</div>
</div>
<!-- Gender Block -->
<div class="form-group">
<label for="select" class="col-lg-2 control-label">Gender</label>
<div class="col-lg-7">
<select id="select" class="form-control" formControlName="gender">
<option *ngFor = "let g of genderList"
[value] = "g"> {{g}}
</option>
</select>
</div>
</div>
<!--- Terms and conditions Block -->
<div class="form-group checkbox">
<div class="col-lg-6 ">
<input type="checkbox" formControlName="terms"> Confirm that you've read the Terms and
Conditions
</div>
<!--- Terms error block -->
<div *ngIf="(terms.invalid && terms.touched)"
class="col-lg-4 text-danger">
Please accept the Terms and conditions first.
</div>
</div>
<!--- Buttons Block -->
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button type="reset" class="btn btn-default">Cancel</button>
<button type="submit" [disabled] = "!signupForm.valid" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
<!--- Log the FormGroup values to see if the binding is working -->
</div>
</div>
.custom-row {
margin: 20px 20px 20px 10%;
padding: 30px 20px 20px 10px;
text-align: left;
}
.form-group {
padding-top: 40px;
}
.control-label {
padding-top: 5px;
}
.checkbox {
margin-left: 5%;
}
import { AbstractControl } from '@angular/forms';
export function passwordMatch
(control: AbstractControl):{[key: string]: boolean} {
//Grab pwd and confirmPwd using control.get
const pwd = control.get('pwd');
const confirmPwd = control.get('confirmPwd');
// If FormControl objects don't exist, return null
if (!pwd || !confirmPwd) return null;
//If they are indeed equal, return null
if (pwd.value === confirmPwd.value) {
return null;
}
//Else return false
return {
mismatch: true };
}