Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. <ion-header>
  2. <ion-navbar>
  3. <ion-title>
  4. Authorization Form Demo
  5. </ion-title>
  6. </ion-navbar>
  7. </ion-header>
  8.  
  9. <ion-content class="home" padding>
  10. <form [ngFormModel]="authForm" (ngSubmit)="onSubmit(authForm.value)">
  11. <ion-item [class.error]="!username.valid && username.touched">
  12. <ion-label floating>Username</ion-label>
  13. <ion-input type="text" value="" [ngFormControl]="username"></ion-input>
  14. </ion-item>
  15. <div *ngIf="username.hasError('required') && username.touched"
  16. class="error-box">* Username is required!</div>
  17. <div *ngIf="username.hasError('minlength') && username.touched"
  18. class="error-box">* Minimum username length is 8!</div>
  19. <div *ngIf="username.hasError('checkFirstCharacterValidator') && username.touched"
  20. class="error-box">* Username cant't start with number!</div>
  21. <ion-item [class.error]="!password.valid && password.touched">
  22. <ion-label floating>Password</ion-label>
  23. <ion-input type="text" value="" [ngFormControl]="password" ></ion-input>
  24. </ion-item>
  25. <div *ngIf="password.hasError('required') && password.touched"
  26. class="error-box">* Password is required</div>
  27. <div *ngIf="password.hasError('minlength') && password.touched"
  28. class="error-box">* Minimum password length is 8!</div>
  29. <div *ngIf="password.hasError('checkFirstCharacterValidator') && password.touched"
  30. class="error-box">* Password cant't start with number!</div>
  31. <br/><br/>
  32. <button type="submit" class="custom-button" [disabled]="!authForm.valid" block>Submit</button>
  33. </form>
  34. </ion-content>
  35.  
  36. import {Component} from '@angular/core';
  37. import {NavController} from 'ionic-angular';
  38. import {FORM_DIRECTIVES, FormBuilder, ControlGroup, Validators, AbstractControl} from '@angular/common';
  39. import {CustomValidators} from '../validators/CustomValidators';
  40.  
  41.  
  42. @Component({
  43. templateUrl: 'build/pages/form/form.html',
  44. directives: [FORM_DIRECTIVES]
  45. })
  46.  
  47. export class FormPage {
  48.  
  49. authForm: ControlGroup;
  50. username: AbstractControl;
  51. password: AbstractControl;
  52.  
  53. constructor(private navController: NavController, private fb: FormBuilder) {
  54. this.authForm = fb.group({
  55. 'username': ['', Validators.compose([Validators.required, Validators.minLength(8), CustomValidators.checkFirstCharacterValidator])],
  56. 'password': ['', Validators.compose([Validators.required, Validators.minLength(8), CustomValidators.checkFirstCharacterValidator])]
  57. });
  58.  
  59. this.username = this.authForm.controls['username'];
  60. this.password = this.authForm.controls['password'];
  61. }
  62.  
  63. onSubmit(value: string): void {
  64. if(this.authForm.valid) {
  65. console.log('Submitted value: ', value);
  66. }
  67. }
  68. }
  69.  
  70. import { Control, ControlGroup } from "@angular/common";
  71.  
  72. interface ValidationResult {
  73. [key: string]: boolean;
  74. }
  75.  
  76. export class CustomValidators {
  77.  
  78. public static checkFirstCharacterValidator(control: Control): ValidationResult {
  79. var valid = /^d/.test(control.value);
  80. if (valid) {
  81. return {checkFirstCharacterValidator: true};
  82. }
  83. return null;
  84. }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement