Guest User

Untitled

a guest
Nov 12th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. import { Component, OnInit, NgModule, Injector, Injectable } from '@angular/core';
  2. import { FormBuilder, FormControl, Validators, FormArray, FormGroup, AbstractControl, ValidatorFn, AsyncValidator, AsyncValidatorFn, ValidationErrors, NG_ASYNC_VALIDATORS } from '@angular/forms';
  3. import { faCheck } from '@fortawesome/free-solid-svg-icons';
  4. import { Observable } from 'rxjs';
  5. import { UserService } from '../user.service';
  6.  
  7.  
  8.  
  9.  
  10. const passwordValidator: ValidatorFn = (fg: FormGroup): ValidationErrors | null => {
  11. var pw = fg.get('password');
  12. var pw2 = fg.get('passwordre');
  13. if(pw2.value.length == 0){
  14. return null
  15. }
  16. if(pw && pw2 && pw.value !== pw2.value){
  17. pw2.setErrors({'passNoMatch': true});
  18. pw2.markAsTouched();
  19. return { 'passNoMatch': true };
  20. }else{
  21. pw2.setErrors(null);
  22. return null;
  23. }
  24. }
  25.  
  26. const username: ValidatorFn = (control: FormControl): ValidationErrors | null => {
  27. var alias = control
  28. if(alias && /^w+$/.test(alias.value)==false){//if not only numbers and letters
  29. return { 'illegalChars': true };
  30. }else if(alias && /^d+$/.test(alias.value)==true){//If only numbers
  31. return { 'onlyNumbers': true };
  32. }else{
  33. alias.setErrors(null);
  34. return null;
  35. }
  36. }
  37.  
  38.  
  39. @Component({
  40. selector: 'app-create-account',
  41. templateUrl: './create-account.component.html',
  42. styleUrls: ['./create-account.component.scss'],
  43. providers: [UserService]
  44. })
  45.  
  46. export class CreateAccountComponent{
  47. faCheck = faCheck
  48. constructor( private fb: FormBuilder, private userService: UserService) {
  49. this.userService.getUserByAlias('test')
  50. }
  51.  
  52.  
  53. checkUsername(control: FormControl): Promise<any> | Observable<any>{
  54. this.userService.getUserByAlias(control.value)
  55. const promise = new Promise<any>((resolve, reject) =>{
  56. //const user = new UserService();
  57. setTimeout(()=>{
  58. if(control.value === 'test'){
  59. resolve({usernameTaken: true})
  60. }else{
  61. resolve(null)
  62. }
  63. },1500)
  64. })
  65.  
  66. return promise;
  67. }
  68.  
  69.  
  70. usernameCtrl = this.fb.control('', [
  71. Validators.required,
  72. Validators.minLength(4),
  73. username
  74. ], this.checkUsername)
  75. emailCtrl = this.fb.control('', [
  76. Validators.required,
  77. Validators.email
  78. ])
  79. passwordCtrl = this.fb.control('', [
  80. Validators.required,
  81. Validators.minLength(5)
  82. ])
  83. passwordreCtrl = this.fb.control('', [
  84. Validators.required,
  85. Validators.minLength(5)
  86. ])
  87.  
  88. newAccountForm = this.fb.group({
  89. username: this.usernameCtrl,
  90. email: this.emailCtrl,
  91. password: this.passwordCtrl,
  92. passwordre: this.passwordreCtrl
  93. },{ validator: passwordValidator})
  94. }
  95.  
  96. import { Injectable } from '@angular/core';
  97. @Injectable({
  98. providedIn: 'root',
  99. })
  100.  
  101. export class UserService {
  102. getUserByAlias(alias: any){
  103. console.log(alias)
  104. return alias
  105. }
  106. }
  107.  
  108. usernameCtrl = this.fb.control('', [
  109. Validators.required,
  110. Validators.minLength(4),
  111. username
  112. ], this.checkUsername,bind(this))
Add Comment
Please, Sign In to add comment