diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 2326761..fa014fe 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -26,6 +26,9 @@ import { LoginComponent } from './login/login.component'; import { RegisterComponent } from './register/register.component'; import { RegistrationService } from './registration.service'; +import { CreditCardValidator } from './creditcardvalidator.directive' +import { CreditCardValidatorX } from './creditcardvalidatorx.directive' + @NgModule({ @@ -34,7 +37,9 @@ import { RegistrationService } from './registration.service'; GameGridComponent, ViewCardComponent, LoginComponent, - RegisterComponent + RegisterComponent, + CreditCardValidator, + CreditCardValidatorX ], imports: [ BrowserModule, diff --git a/src/app/creditcardvalidator.directive.ts b/src/app/creditcardvalidator.directive.ts new file mode 100644 index 0000000..c65f234 --- /dev/null +++ b/src/app/creditcardvalidator.directive.ts @@ -0,0 +1,31 @@ +import { Directive, forwardRef } from '@angular/core'; +import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms'; + +@Directive({ + selector: '[validCreditCard]', + providers: [ + { provide: NG_VALIDATORS, useExisting: CreditCardValidator, multi: true } + ] +}) +export class CreditCardValidator implements Validator { + + validate(c: FormControl): ValidationErrors | null { + return CreditCardValidator.validateCcNumber(c); + } + + static validateCcNumber(control: FormControl): ValidationErrors | null { + if (!(control.value.startsWith('37') + || control.value.startsWith('4') + || control.value.startsWith('5')) + ) { + // Return error if card is not Amex, Visa or Mastercard + return { creditCard: 'Your credit card number is not from a supported credit card provider' }; + } else if (control.value.length !== 16) { + console.log(control.value) + // Return error if length is not 16 digits + return { creditCard: 'A credit card number must be 16-digit long' }; + } + // If no error, return null + return null; + } +} \ No newline at end of file diff --git a/src/app/creditcardvalidatorx.directive.ts b/src/app/creditcardvalidatorx.directive.ts new file mode 100644 index 0000000..baffce0 --- /dev/null +++ b/src/app/creditcardvalidatorx.directive.ts @@ -0,0 +1,56 @@ +import { Directive, forwardRef } from '@angular/core'; +import { NG_VALIDATORS, AbstractControl, ValidationErrors, Validator, FormControl } from '@angular/forms'; +import { RegistrationService } from './registration.service'; + +@Directive({ + selector: '[validCreditCard]', + providers: [ + { provide: NG_VALIDATORS, useExisting: CreditCardValidatorX, multi: true } + ] +}) +export class CreditCardValidatorX implements Validator { + + constructor( + private registrationService2: RegistrationService + ){ } + + validate(c: FormControl): ValidationErrors | null { + return CreditCardValidatorX.validateCcNumber(c); + } + + + validateUserName( control: FormControl ){ + + this.registrationService2.validateUserName( control.value ).subscribe( data => { + console.log( data ); + if( control.value != data.username ){ + return{ + validUserName: true + }; + } + return null; + + }); + + } + + static validateCcNumber(control: FormControl): ValidationErrors | null { + if (!(control.value.startsWith('37') + || control.value.startsWith('4') + || control.value.startsWith('5')) + ) { + // Return error if card is not Amex, Visa or Mastercard + return { creditCard: 'Your credit card number is not from a supported credit card provider' }; + } else if (control.value.length !== 16) { + console.log(control.value) + // Return error if length is not 16 digits + return { creditCard: 'A credit card number must be 16-digit long' }; + } + // If no error, return null + return null; + } + + + + +} \ No newline at end of file diff --git a/src/app/register/register.component.html b/src/app/register/register.component.html index 24345a3..d996c76 100644 --- a/src/app/register/register.component.html +++ b/src/app/register/register.component.html @@ -24,7 +24,7 @@ - { @@ -96,6 +103,7 @@ export class RegisterComponent implements OnInit { }); } + */